Java program to print prime numbers below 100

Java Programming Java8Object Oriented Programming

Any whole number which is greater than 1 and has only two factors that is 1 and the number itself, is called a prime number. Other than these two number it has no positive divisor. For example

7 = 1 × 7

Few prime numbers are − 1, 2, 3, 5, 7, 11 etc.

Algorithm

  • 1. Take integer variable A
  • 2. Divide the variable A with (A-1 to 2)
  • 3. If A is not divisible by any value (A-1 to 2), except itself it is a prime number.
  • 4. Repeat this for all the numbers starting from 2 to the required limit.

Example

Live Demo

public class First100Primes {
   public static void main(String args[]){

      for(int i = 2; i<=100;){
         int prime = 1;
         for(int loop = 2; loop <=i; loop++) {
            if((i % loop) == 0 &amp;&amp; loop!=i) {
               prime = 0;
            }
         }
         if (prime != 0){
            System.out.println(i+" is a prime number");
            i++;
         }
         else
         i ++;
      }
   }
}

Output

2 is a prime number
3 is a prime number
5 is a prime number
7 is a prime number
11 is a prime number
13 is a prime number
17 is a prime number
19 is a prime number
23 is a prime number
29 is a prime number
31 is a prime number
37 is a prime number
41 is a prime number
43 is a prime number
47 is a prime number
53 is a prime number
59 is a prime number
61 is a prime number
67 is a prime number
71 is a prime number
73 is a prime number
79 is a prime number
83 is a prime number
89 is a prime number
97 is a prime number
raja
Published on 25-Apr-2018 11:47:57
Advertisements