Java – Print Prime numbers between specified range

In this article, we will write a simple Java program to print Prime numbers between the specified range or interval

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 print Prime numbers between specified range/interval using 2 different loops,
    1. using for loop
    2. using while loop

1.1 Using for loop

We will iterate through for-loop to find prime numbers for the specified range and invoke method for each numbers and it does the following task,

  • 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

PrimeNumbersBetweenRangeUsingForLoop.java

package in.bench.resources.numbers;

public class PrimeNumbersBetweenRangeUsingForLoop {

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

		System.out.println("Prime numbers between 1 & 100 :- \n");

		// find prime numbers between 1 and 100
		for(int num = 1; num <= 100; num++) {

			// check prime or not
			if(checkPrimeOrNot(num)) {
				System.out.print(num + " ");
			}
		}
	}


	/**
	 * 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;

		// Numbers 0 & 1 aren't Prime Numbers
		if(0 == number || 1 == number) {

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

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

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

		// return 
		return isPrime;
	}
}

Output:

Prime numbers between 1 & 100 :- 

Numbers 0 & 1 aren't Prime Numbers
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

1.2 Using while loop

We will iterate through while-loop to find prime numbers for the specified range and invoke method for each numbers and it does the following task,

  • 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

PrimeNumbersBetweenRangeUsingWhileLoop.java

package in.bench.resources.numbers;

public class PrimeNumbersBetweenRangeUsingWhileLoop {

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

		int num = 100;

		System.out.println("Prime numbers between 100 & 199 :- \n");

		// find prime numbers between 100 and 199
		while(num <= 199) {

			// check prime or not
			if(checkPrimeOrNot(num)) {
				System.out.print(num + " ");
			}

			// increment num by 1
			num++;
		}
	}


	/**
	 * 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:

Prime numbers between 100 & 199 :- 

101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199

Happy Coding !!
Happy Learning !!

Java – Check whether number is Prime or Not ?