1 static boolean isPrime(int n){
2 boolean result=true;
3 if(n <= 1) return false;
4 for(int i = 2; i <= n/2; i++){ //n/2 because the largest factor of any number is either it's half or less than it
5 if(n % i == 0){
6 result = false; //if some number divides it, then prime=false;
7 break;
8 }
9 }
10 return result;
11 }
1public class Prime {
2
3 public static void main(String[] args) {
4
5 int num = 29;
6 boolean flag = false;
7 for(int i = 2; i <= num/2; ++i)
8 {
9 // condition for nonprime number
10 if(num % i == 0)
11 {
12 flag = true;
13 break;
14 }
15 }
16
17 if (!flag)
18 System.out.println(num + " is a prime number.");
19 else
20 System.out.println(num + " is not a prime number.");
21 }
22}
1public class PrimeNumberInJava
2{
3 public static void main(String[] args)
4 {
5 int a, b = 0, flag = 0;
6 int number = 17;
7 b = number / 2;
8 if(number == 0 || number == 1)
9 {
10 System.out.println(number + " is not prime number");
11 }
12 else
13 {
14 for(a = 2; a <= b; a++)
15 {
16 if(number % a == 0)
17 {
18 System.out.println(number + " is not prime number.");
19 flag = 1;
20 break;
21 }
22 }
23 if(flag == 0)
24 {
25 System.out.println(number + " is prime number.");
26 }
27 }
28 }
29}