Java Examples
Java ExamplesPrograms with output

Java Program to Check Prime or Not


Example:
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

Share this page on:

Was this page helpful ?

Let us know how we did!

Subscribe Email Updates

to get latest update