Search Your Topic

config
Thursday, 18 May 2017 03:23

Prime Number Program in Java

Written by
Rate this item
(0 votes)

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. The 2 is the only even prime number because all the other even 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
Read 270 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

Java Training

Facebook