This Java Example if for beginners to check if entered number is prime i.e. number which is divisible by only two numbers first 1 and second itself. If any number is divisible by 3 or more numbers, it is not a prime number.

```
import java.util.Scanner;
public class Prime {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int number = keyboard.nextInt();
boolean flag = false;
for(int i = 2; i <= number/2; ++i)
{
// condition for nonprime number
if(number % i == 0)
{
flag = true;
break;
}
}
if (!flag)
System.out.println(number + " is a prime number.");
else
System.out.println(number + " is not a prime number.");
}
}
```

## No comments:

## Post a Comment