Thursday, March 26, 2009

java code to find prime numbers

import java.io.*;
public class prime
{
public static void main(String [] y)
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number.. ");
String x=null;
boolean b=true;
try
{
 x=br.readLine();
}
catch(IOException io)
{
System.out.println(io);
}
int z=Integer.parseInt(x);
for(int i =2;i<=Math.sqrt(z);i++)
{
if(z%i==0)
b=false;
}
if(b==true)
System.out.println("number is prime "+z);
else
System.out.println("number is not prime "+z);
}
}

0 comments: