java program for prime number

Java program for prime number



What is a prime number?

Prime number is a number which is greater than one and divided by one or by itself.


 2, 3, 5, 7, 11, 13, 17.... are some of the example of prime numbers.


Now in below program we will see the program to print prime number in java.

java program for prime number

public class PrimeNumbersList {  
           public static void main(String[] args) {   
                   int numCnt = 20, count;   
                  for (int cntr = 1; cntr  <= num; cntr ++) {   
                             count = 0;   
                           for (int innerCntr = 2; innerCntr  <= cntr  / 2; innerCntr ++) {    
                                    if (cntr  % innerCntr  == 0) {     
                                              count++;     
                                                break;    
                                    }   
                        }    
                     if (count == 0) {    
                             System.out.println("Prime number : "+cntr );   
                     }  
              } 
        }
}
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment