logo image

Java program to find the given number is Prime number or not



Java program which takes a number as input and prints its a Prime number or not

import java.util.Scanner;

public class PrimeNumber {

	public static void main(String[] args) {

		System.out.println("Please enter a number: ");

		Scanner in = new Scanner(System.in);
		int input = in.nextInt();
		in.close();

		boolean isPrimeNumber = true;
		for (int i = 2; i <= input - 1; i++) {
			if (input % i == 0) {
				isPrimeNumber = false;
			}
		}

		if (isPrimeNumber == true) {
			System.out.println("It's prime number");
		} else {
			System.out.println("It's not prime number");
		}

	}
}

Output:

Please enter a number: 
27
It's not prime number