Java – Check whether number is Prime or Not ?

In this article, we will write a simple Java program to check whether a number is Prime number or Not

1. Prime Number programs

  • A number is Prime number which is divisible by 1 or itself only
  • 0 & 1 aren’t Prime Numbers
  • 2 is the only Even Prime number
  • We will check whether a number is Prime or Not using 2 different loops,
    1. using for loop
    2. using while loop

1.1 Using for-loop

  • Initially we will declare a flag isPrime and set to true assuming given number is prime
  • Iterate using for loop,
    • starting from 2
    • up to given number/2
    • increment by 1 in every iteration
  • During iteration inside for-loop, we will check whether remainder is zero by modulo of given number by iterating numbers from 2 till given number/2
  • If condition succeeds then set the flag to false and break the for-loop i.e., given number is not Prime number
  • Finally, returning isPrime flag to calling method to print to console

CheckNumberIsPrimeUsingForLoop.java

package in.bench.resources.numbers;

public class CheckNumberIsPrimeUsingForLoop {

	// main() method
	public static void main(String[] args) {

		System.out.println("Whether 7 is prime ? " + checkPrimeOrNot(7));
		System.out.println("Whether 20 is prime ? " + checkPrimeOrNot(20));
		System.out.println("Whether 97 is prime ? " + checkPrimeOrNot(97));
		System.out.println("Whether 100 is prime ? " + checkPrimeOrNot(100));
	}


	/**
	 * This method checks whether input number is prime or Not using for loop
	 * 
	 * - 0 & 1 aren't Prime Numbers
	 * - 2 is the only EVEN Prime number
	 * - All even numbers are Prime numbers
	 * - Prime number is divisible by itself or 1
	 * 
	 * @param number
	 * @return
	 */
	public static boolean checkPrimeOrNot(int number) {

		// local variable
		boolean isPrime = true;

		if(0 == number || 1 == number) {

			System.out.println("Numbers 0 & 1 aren't Prime Numbers");
			isPrime = false;
		}
		else {

			for(int i=2; i <= number / 2; i++) {

				if(number % i == 0) {
					isPrime = false;
					break;
				}
			}
		}

		// return 
		return isPrime;
	}
}

Output:

Whether 7 is prime ? true
Whether 20 is prime ? false
Whether 97 is prime ? true
Whether 100 is prime ? false

1.2 Using while-loop

  • Initially we will declare a flag isPrime and set to true assuming given number is prime
  • Declare another variable i of type integer with initial value of 2
  • Iterate using while-loop, by checking whether the value of i is less-than or equal-to number/2
  • During iteration inside while-loop, we will check whether remainder is zero by modulo of given number by iterating numbers from 2 till given number/2 and also increment variable i by 1 in every iteration at the end
  • If condition succeeds then set the flag to false and break the while-loop i.e., given number is not Prime number
  • Finally, returning isPrime flag to calling method to print to console

CheckNumberIsPrimeUsingWhileLoop.java

package in.bench.resources.numbers;

public class CheckNumberIsPrimeUsingWhileLoop {

	// main() method
	public static void main(String[] args) {

		System.out.println("Whether 7 is prime ? " + checkPrimeOrNot(7));
		System.out.println("Whether 20 is prime ? " + checkPrimeOrNot(20));
		System.out.println("Whether 97 is prime ? " + checkPrimeOrNot(97));
		System.out.println("Whether 100 is prime ? " + checkPrimeOrNot(100));
	}


	/**
	 * This method checks whether input number is prime or Not using while loop
	 * 
	 * - 0 & 1 aren't Prime Numbers
	 * - 2 is the only EVEN Prime number
	 * - All even numbers are Prime numbers
	 * - Prime number is divisible by itself or 1
	 * 
	 * @param number
	 * @return
	 */
	public static boolean checkPrimeOrNot(int number) {

		// local variable
		int i = 2;
		boolean isPrime = true;

		if(0 == number || 1 == number) {

			System.out.println("Numbers 0 & 1 aren't Prime Numbers");
			isPrime = false;
		}
		else {

			while(i <= number / 2) {

				if(number % i == 0) {
					isPrime = false;
					break;
				}

				// increment value by 1
				i++;
			}
		}

		// return 
		return isPrime;
	}
}

Output:

Whether 7 is prime ? true
Whether 20 is prime ? false
Whether 97 is prime ? true
Whether 100 is prime ? false

Happy Coding !!
Happy Learning !!

Java – Print Prime numbers between specified range
Java - How to Reverse a Number ?