Java Program - Prime Number

I would like to write some useful codes for the java beginners also in my blog. In the beginning, I faced problem in learning java. There are lots of online tutorials available on java also for the student and java beginners.

Here is the code to find prime numbers between 1 to user defined number. In this program, I am using "for loop" 2 times. First loop start from 1 to user enterned number and another loop divide the number by 2.

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("is not Prime!");

break;

}
}
if(i == num){

System.out.println("is Prime number!");
}
}
}


0 comments: