fastread homefastrread library fastread menu

Java : Prime Number Program in Java

Tutorial by:Arena Buocel      Date: 2016-04-29 23:03:36

❰ Previous Next ❱

Prime number in Java: Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

Note: 0 and 1 are not prime numbers. 2 is the only even prime number because all the numbers can be divided by 2.

Let's see the prime number program in java. In this java program, we will take a number variable and check whether the number is prime or not.

 
  1. class PrimeExample{  
  2.  public static void main(String args[]){  
  3.   int i,m=0,flag=0;    
  4.   int n=17;//it is the number to be checked  
  5.   m=n/2;    
  6.   for(i=2;i<=m;i++){    
  7.    if(n%i==0){    
  8.    System.out.println("Number is not prime");    
  9.    flag=1;    
  10.    break;    
  11.    }    
  12.   }    
  13.   if(flag==0)    
  14.   System.out.println("Number is prime");    
  15. }  
  16. }  

Output:

Number is prime

❰ Previous Next ❱


Java

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website