Java program to Check Prime number

In Java, to check whether a given number is prime or not we can use loop concepts such as while loop and for loop.

Prime number is a number that is divisible by 1 and itself. For example 2, 3, 5, 7, 11,….. are prime numbers. But 4, 6, .. are not because it is divisible by other number as well.

In this article we will learn how to check prime number in Java through an example, detailed logic and program explanation for better understanding.

Required Knowledge

Problem Statement

We need to write a Java program to check whether a given number is Prime or not. The program should take one number as input and for each input, the program should output either “Prime number” if the number is a prime number or “Not a Prime number” if it is not.

Let’s see an example,

Example:

For input number 29, Output should be “Prime Number”.

Explanation:

Input number: 29

29 is exactly divisible by 1 and 29.

As 29 satisfies the prime number condition, 29 is a Prime number.

If we check with another number like 30, it will divisible by 2, 3, 5, 6, 10, 15 and 30. So the number 30 is not a prime number.

Logic to check whether a given number is Prime or not

  • Take an integer input from the user. Initialize boolean variable isPrime to true (assuming the number to be prime). Then using an if statement, it checks if the number is less than or equal to 1 (not prime). If num not equal to 1, it uses a loop (while or for loop) to check if the number is divisible by any number from 2 to num/2.
  • If it finds a divisor, it sets isPrime to false. Based on the value of isPrime, it prints whether the number is prime or not using an if statement.

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

Program 1: Using while loop

In the below program we are using while loop to check whether a given number is prime or not in Java.

import java.util.Scanner;
public class PrimeCheck {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter a number");
		int num = s.nextInt();
		boolean isPrime = true;
		if (num <= 1) {
          isPrime=false;
		}
		if(num!=1) {
			int i = 2;
			while (i < num/2) {
				if (num % i == 0) {
					isPrime = false;
					break;
				}
				i++;
			}
		}
		if (isPrime)
			System.out.println("It is prime number");
		else
			System.out.println("It is not a prime number");
		s.close();
	}
}

Output:

Enter a number
1
It is not a prime number

Program explanation to check whether given number is prime or not

  1. The above program asks user to enter a number.
  2. Program reads the entered number using a Scanner.
  3. Program will initializes a boolean variable isPrime to true.
  4. If the entered number is less than or equal to 1, isPrime is set to false.
  5. It then checks divisibility of the number by integers from 2 up to less than the half of number.
  6. If the number is divisible by any of these integers, isPrime is set to false, and the loop breaks.
  7. Finally, it prints whether the entered number is prime or not based on the value of isPrime.
  8. The program closes the Scanner to release system resources.

Program 2: Using for loop

In the below program we are using for loop to check if a number is prime number or not in Java.

import java.util.Scanner;
public class PrimeCheck {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter a number");
		int num = s.nextInt();
		boolean isPrime = true;
		if(num<=1) {
			isPrime=false;
		}
		if (num != 1) {
			int i = 2;
			for (; i < num/2; i++) {
				if (num % i == 0) {
					isPrime = false;
					break;
				}
			}
		}
		if (isPrime)
			System.out.println("It is prime number");
		else
			System.out.println("It is not a prime number");
		s.close();
	}
}

Output:

Enter a number
137
It is prime number

Java program explanation to check whether given number is prime or not

  1. The program asks the user to enter a number.
  2. Program reads the entered number using a Scanner.
  3. It will initializes a boolean variable isPrime to true.
  4. If the entered number is less than or equal to 1, isPrime is set to false.
  5. Else, if the entered number is not equal to 1, it iterates through integers from 2 up to less than half of the entered number.
  6. Inside the loop, it checks if the entered number is divisible by the current integer (i).
  7. If the entered number is divisible by any of these integers, isPrime is set to false, and the loop breaks.
  8. Finally, it prints whether the entered number is prime or not based on the value of isPrime.
  9. The program closes the Scanner to release system resources.

Conclusion

In this article, we have learnt how to check whether a number is prime or not in Java. We have seen the logic and implemented the program using while loop and for loop. Hope this article helped you in the understanding of the program.

You can also check our another awesome tutorials:

Happy Java Programming!!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.