PROGRAM FOR PRIME NUMBER

Prime number logic: a number is prime if it is divisible only by one and itself.

Remember two is the only even and also the smallest prime number.

First few prime numbers are 2, 3, 5, 7, 11, 13, 17....etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Scanner;
public class prime
{
    public static void main(String args[])
    {
        int num, i, count=0;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter any integer (limit upto 2^31-1) value:");
        num = scan.nextInt();
         for(i=2; i<num; i++)
        {
            if(num%i == 0)
            {
                count++;
                break;
            }
        }
        if(count == 0)
        {
            System.out.print("is prime");
        }
        else
        {
            System.out.print("is not a prime ");
        }
    }
}

Output : Prime Number
IPprime