Java Program to Check Prime or Not


-

-

Example:

Copied
import java.util.Scanner;

public class CheckPrimeOrNot
{
    public static void main(String args[])
    {
        int num, i, count=0;
        Scanner scan = new Scanner(System.in);
		
        System.out.println("Enter any Number: ");
        num = scan.nextInt();
		
        for(i=2; i<num; i++)
        {
            if(num%i == 0)
            {
                count++;
                break;
            }
        }
        if(count == 0)
        {
            System.out.println("It is Prime Number");
        }
        else
        {
            System.out.println("It is not Prime Number");
        }
    }
}

Output:

Enter any Number:
55
It is not Prime Number