Home Java Beginners Find out the prime number
Questions:Ask|Latest


 
 

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

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

Ask Questions?    Discuss: Find out the prime number   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Dissanayake
June 1, 2011
how (i==num)

I am a beginner to java and I have problem with this code, how the (i == num) thats my problem. because i's value getting after the loop i<num. hows it's equle to num. please send me the reply
faisal
June 24, 2011
how is working this loop

i have this error from this program, Exception in thread "main" java.lang.NoClassDefFoundError: Prime_number/java
Rinku Rajole
August 13, 2011
Thanks

i will try this program lots of time but it give error. Butnow my problem is solved so thanks.
Apurba
August 17, 2011
Another Solution

import java.util.Scanner; public class Prime_number { public static void main(String[] args) { Scanner k = new Scanner (System.in); int i=2; int a = k.nextInt(); while (i < a ) { int n = a%i; { if (n==0) { System.out.println("Not Prime"); break; } } i++; } if (i == a) { System.out.println("Prime number!"); } } }
jacinth1215
September 9, 2011
prime

what if i like to use "while"?
Anonymous
February 19, 2013
Code inefficient

This code is not efficient
Debasish Nanda
October 10, 2011
formula to find out prime number

sir/medam please tell me the formula to find out the prime number
zs
October 20, 2011
Assignments

good code
naga
October 26, 2011
doubt

in this check prime number program how "i" value becomes 11 ie if(i==num) { PRINT(is prime); }
krishnakant
November 15, 2011
computer science

this very useful for bunkers
Nabil
December 6, 2011
ISC Computer Applications - Grade 11

In the last "if..." condition, the condition should be if (num - i == 1), as you are running the for loop from i = 2, to i < num.
Nadav David
December 25, 2011
Thanks

It helped me a lot
prajot
January 17, 2012
java

how the loops and break are used ??
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