Java program to check whether a number is prime or not?

Home / Uncategorized / Java program to check whether a number is prime or not?

Java program to check whether a number is prime or not?

import java.util.Scanner;
public class CheckPrimeNumber 
{
    public static void main(String args[])
    {
        int n,p=1;
        System.out.print("Enter any number:");
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        for(int i=2;i<=n;i++)
        {
            if(n%i==0)
            {
                p++;
            }
        }
        if(p==2)
        {
            System.out.println("This is Prime Number.");
        }
        else
        {
             System.out.println("This is not a Prime Number.");
        }
    }
}

Output:

Enter any number:11
This is Prime Number.

Watch video of this program

Leave a Reply

Your email address will not be published. Required fields are marked *