Prime Number

Prime Number is a number which is only divisible by 1 and the number itself i.e., it has no factors other than 1 and itself.

Example 1: 7 is only divisible by 1 and itself only. Hence it is a Prime Number.

Example 2: 4 is divisible by 1 and 2 and 4. Hence it is not a Prime Number.

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