Find out the prime number

In this section , you will learn how to find out whether a given number is prime or not.


Find out the prime number


This lesson of Java programming language will teach you the coding to find out whether a given number is prime or not. Here we have used the 'for loop' statement and given the required condition for a prime number. As we know, a prime number is only divided by 1 and itself, in other words it has no other factorial other than 1 and the number itself.  

Here, first make a class and named as "Primenumber" and take an integer as num=11, and define an integer 'i' as the integer other than 1 and the given number. That means, i>2 and i<num. Now apply this in the "for loop" statement and define an integer n=num/i as given below in the example. Now apply the "if" condition and if the reminder of the earlier equation comes "0", then the result will be not prime. Again the loop system will check the above condition until it has not satisfied from the starting point(2) to the end(10). Here under this loop we have to use the "break" statement for unnecessary checking further one point where the reminder comes zero(0). 

Now after checking the whole condition, if the reminders does not come "zero", then we have to again apply the "if" condition and check whether i=num or not. If it is true then number (num) is prime. As we have taken here as num=11, then after compiling and running the program, the result will show that num is prime number.  

Here is the code program: 

class Prime_number {
  public static void main(String[] args) {
  int num = 11;
  int i;
  for (i=2; i < num ;i++ ){
  int n = num%i;
  if (n==0){
  System.out.println("not Prime!");
  if(i == num){
  System.out.println("Prime number!");

Download this page


Share on Google+Share on Google+

Find out the prime number

Posted on: June 4, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Find out the prime number   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Bertie Wheen
March 13, 2012

instead of for (i=2; i < num ;i++ ) use for (i = 2; i < (int)Math.sqrt(num); i++)
March 23, 2012

Hey, just a question, but i could never equal the prime number because its a simple < symbol in the for loop right? if not please explain. is it because the ++ is added during the loop?
sandeep kamboj
April 2, 2012
prime no. programme

its very good explanation. thnks for good work
June 11, 2012
want to learn from mistake

why i can't use b's value out side the loop,where b is global. class a { public static void main(String arg[]) { int a=Integer.parseInt(arg[0]); int i,b; for (i=2;i<a;i++) { b= a%i; } if(b==0) { System.out.println( a +" is not prime"); } else { System.out.println(a+"Prime number!"); } } }
Bobby k bose
June 14, 2012
Java program of prime numbers

Your java program of finding "prime numbers " did really helped me. Thanks for the program!
June 16, 2012
this code

Dude, this is a really speculative piece of code. The only thing it attempts to validate is the integer 11 as a prime number. It does that in both parts of your conditional statement, even though you used the string words "Not Prime"!! What about the other prime numbers that exist in the variable i, that is, 3, 5 and 7. That piece of code fails to capture them.
August 20, 2012

class abc { void display() { int a=11; int x; int c=0; for(x=1;x<=a;x++) { int d=(x%a==0); c++; } if(C==2) system.out.println("prime"); else system.out.println("not prime"); } }
September 16, 2012

how can i the number is prime
January 6, 2014
prime no.

Thank u so much.I got my work done.