Java Program to display first 100 prime numbers using for loop

Here we are explaining how to write a java program to display the first 100 prime numbers. This program will make use of loops in Java, so we recommend the following topics for a better understanding of this program.

What is a prime number?

Prime numbers are numbers that are greater than one and that number has only two factors. The first factor is 1 and the second factor is that number itself.

Example:3 5 7 11 13 17 etc.

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

We have another program to print prime numbers between 1 to 100 in our Java program collection, also refer to it for more ideas.

The image below shows some numbers and their divisors/factors. As you can see number 2,3,5,7 has only 2 divisors that is 1 and the number itself, these kinds of numbers are called prime numbers. To find a prime number in our program we use this technique of finding factors. 

Numbers that have only 2 divisors are prime and others are composite numbers.

Prime Number

How to implement the java program to display the first 100 prime numbers?

First, we have to declare the class PrmNum.Then declare the variables. Here we are using a while loop and for loop to implement this program. The while loop is used to count till 100 prime numbers are printed. The for loop is used to check if the numbers from 2 are prime or not. Numbers are kept in the variable i and divisors in j, then check if i mod j equals zero(ie., j is a factor of i) for each j from 1 to number less than i, if the condition is satisfied then increment the counter variable fct_count by one. After exiting from the for loop, we check the value of the fct_count, If it's 2 then we will display that number ie., factors count 2 means, the number is prime. Also, increment the prime number counter prm_count by one. And continue this process with the next number for that increment i by one. 

Prime Number

ALGORITHM

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

STEP 2: Open the main() to start the program, and declare the integer variables  i, j,fct_count,prm_count

STEP 3: Initialise i=2 and prm_count=0, where i kept the numbers for prime check and prm_count the count of prime numbers

STEP 4: Start a while loop with condition prm_count != 100 to print 100 prime numbers. If the condition is true then do steps 5,6,8,9.

STEP 5: Set value of  fct_count=0 which stores the count of factors of a number in i

STEP 6: By using a for loop with j from i to 1, decrementing j by 1 do step 7.

STEP 7: If i % j==0 then increment the fct_count by one

STEP 8: Check the value in fct_count is 2, if yes then i is prime number print it and increment  prm_count by one.

STEP 9: Increment i to next number

Java Source Code

                                          import java.util.Scanner;
public class PrmNum {
    public static void main(String[] args) {
        int i=2, j,fct_count,prm_count = 0;

        System.out.println("First 100 prime numbers are :");
        while(prm_count!=100){ // print 100 prime numbers
            fct_count=0;
            for (j = i; j >= 1; j--) { // find the factors of number i
                if (i % j == 0) { 
                    fct_count = fct_count + 1; // a factor found, increment the factor count
                }
            }
            if (fct_count == 2){ // only 2 factors, it's prime so print i and increment the prime number count
                System.out.print("" + i + " ");
                prm_count = prm_count + 1;
            }
            i= i+1;
        }
    }
}
                                      

OUTPUT

First 100 prime numbers are :
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 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541