Tuesday, February 10, 2015
Prime Number Checking in java
public class PrimeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int number = 2; number<=20; number++){
//print prime numbers only
boolean r=false;
for(int i=2; i<number; i++){
if(number%i == 0){
r=true;
System.out.println(number+" is not prime");
break;
}
else
{
r=false;
}
}
if(r==false){
System.out.println(number+" is prime");
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int number = 2; number<=20; number++){
//print prime numbers only
boolean r=false;
for(int i=2; i<number; i++){
if(number%i == 0){
r=true;
System.out.println(number+" is not prime");
break;
}
else
{
r=false;
}
}
if(r==false){
System.out.println(number+" is prime");
}
}
}
---------------------------------------------------------------------------
Output:
----------------------------------------------------------------------------
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.