java program to print prime numbers from 1 to 100

Java program to find prime numbers between 1 and 100:

public class PrimeNumberSeries{

   public static void main(String[] args) {              

      int lastNumber = 100;
      System.out.println("Prime numbers are in between 1 to" + lastNumber)
                for(int i=1; i < 100; i++){       //loop start from 1 to last number one by one
                        boolean isPrime = true;
                        for(int j=2; j < i ; j++){        //check to see if the number is prime or not
                                if(i % j == 0){
                                        isPrime = false;
                                        break;
                                }
                        }
                        if(isPrime)
                                System.out.print(i + " ");                           // print the value
                }
        }
}

Output:
Prime numbers are in between 1 to 100                                                     
1 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                                                                          

1 comments:


EmoticonEmoticon