Tuesday, July 28, 2009

Java help.............................

can any one explain what this means taken from this feel free to give an easier program














class Prime


{


public static void main(String[] args){


boolean flag=true;


int i=4;


int c=0;


for(c=2;c%26lt;i;c++){


if((i%c)==0)


{


flag=false;


break;


}


else


flag=true;


}


if(flag)


System.out.println("Prime no");


else


System.out.println("Not a Prime no.");


}


}

Java help.............................
This program basically checks if an integer is a prime number. i is the number that you are checking, in this case 4.


The for loop checks if 4 is divisible by any numbers less than itself (except for 1 of course). If 4 is divisible with no remainders, then 4 is NOT a prime number.





A modification that you could do to make the program run faster for larger i values, is to loop in the for loop only until i/2.


No comments:

Post a Comment