Tuesday, 22 December 2015

Java program to check if the number is Prime

Program:

package com.susanto;

public class PrimeNumberCheck {

       public static void main(String[] args) {
             int i, m = 0, flag = 0;
             int numToCheck = 15;
             m = numToCheck / 2;
             for (i = 2; i <= m; i++) {
                    if (numToCheck % i == 0) {
                           System.out.println("Number is not prime");
                           flag = 1;
                           break;
                    }
             }
             if (flag == 0)
                    System.out.println("Number is prime");
       }
}
Output:

Number is not prime

Hence the solution to our requirement.


Thanks & Regards,
Susanto Paul