Home Java Beginners Find out the prime number

 
 

Share on Google+Share on Google+

Find out the prime number

Advertisement
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!");
  break;
  }
  }
  if(i == num){
  System.out.println("Prime number!");
  }
  }
}

Download this page

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2007

Related Tutorials

Discuss: Find out the prime number   View All Comments

Post your Comment


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

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

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
rahul
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!
Raymond
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.
chetan
August 20, 2012
upleaseshutup

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"); } }
mohammad
September 16, 2012
maths

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

Thank u so much.I got my work done.
DMCA.com