Java Program to display prime numbers between 1 to 100

Here in this Java program, we are explaining how to print the prime numbers between 1 to 100. It can be easily implemented using loops in Java. So for a better understanding of the program, we recommend you to learn the basic topics of Java programming listed below

What is a prime number?

Before going to the program we need to know what is a prime number. Prime numbers are numbers that are greater than one and have only two factors which are 1 and the number itself. If a number has factors of more than 2 then it won't be a prime number or we can say it is a composite number.

For example, 3 5 7 11 13, etc. are the Prime numbers, it has only 2 factors.

Note: Please note that number 1 is not a prime number, it has only one factor, a prime number will have 2 factors.

Here we are going to find the prime numbers between 1 and 100. We have another program to print first 100 prime numbers in our Java program collection. You can also refer to that program for next-level understanding. There are 25 prime numbers lying in between 1 and 100. Let's have a look at how to find them.

How to check the given number is a prime number or not?

How to check the given number is a prime number or not?

From the above example, we can understand that divisors/factors of a number help us to check a number is prime or not. Note that a factor is a number that will completely divide another number without any remainder left. So,

  • Take a number that is greater than 1 from the user (Already know 1 is not prime)
  • Find all factors of user input number
  • Check how many factors are available. If there are more than 2 factors then the number is not prime else it is a prime number.

How to implement the java program to display prime numbers from 1 to 100?

To implement this program in Java we make use of a nested for loop. First, we have to declare the public class PrimNum.Then declare required variables. Use a for loop to iterate from i= 2 to 100 as we need to print the numbers between 1 to 100. Set the value of count to 0, it is the count of factors of a number. Now start another for loop from j=i to 1 to check the number i is divisible by j or not by a mod % operator, i % j will give a 0 remainder if j can divide i completely. If this happens increment the count by 1. After exiting from the inner loop, we check the value of the count, If it is 2 then we will display that number ie., count 2 means the number has factors 1 and that number itself, thus it's a prime number. Continue the process till i reaches 100

How to check the given number is a prime number or not?


STEP 1: Declare the class PrimNum with a public modifier.

STEP 2: Open the main() to start the program, and Declare the integer variables i, j, count

STEP 3: Use a for loop with condition i <= 100, do steps 4,5,7

STEP 4: Set value of count=0

STEP 5: By using another for loop with condition j >= 1, do step 6.

STEP 6: If i % j == 0 then increment the count by one.

STEP 7: If count is equal to 2, it will be prime then print i

Java Source Code

                                          import java.util.Scanner;
public class PrimNum {
    public static void main(String[] args) {
        int i, j,count;

        System.out.println("Prime numbers between 1 to 100 :");
        for (i = 2; i <= 100; i++) {
            count = 0;
            for (j = i; j >= 1; j--) {
                if (i % j == 0)
                    count = count + 1;
            if (count == 2)
                System.out.print("" + i + " ");


Prime numbers between 1 to 100 :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97