Java Program to check a number is prime or not


In this program, You will learn how to check a number is prime or not in Java.


Some list of prime numbers is : 2, 3, 5, 7, 11


Example: How to check a number is prime or not in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        int n, i, p = 1;

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a Number :");
        n = sc.nextInt();

        for (i = 2; i < n; i++) {
            if (n % i == 0) {
                p = 0;
                break;
            }
        }

        if (p == 1){
          System.out.println("\nThe Number is Prime :" + n);
        }else{
          System.out.println("\nThe Number is Not Prime :" + n);
        }

    }
}

Output:

Enter a Number :13
The Number is Prime :13