Java program to print all Prime numbers in a given range

In Java, to print all Prime numbers in a given range we can use loop concepts such as while loop and for loop.

Prime numbers are numbers which are divisible by 1 and the number itself.

If the two input numbers 1 and 5, then the resulting output should be “2, 3 and 5”.

In this article, we are focusing on learning through examples, detailed logic and program explanation for better understanding.

Required Knowledge

Problem Statement

We have to write a java program to print all Prime numbers in a given range. The program should take two input values as range and should print the all prime numbers in the given range. This action will be done in our program. Let’s see an example,

Example:

For input numbers 1 and 7, output should be 2 3 5 7

Explanation

Input integer number: 1

Input integer number: 7

2 3 5 and 7 are divisible by 1 and their self but 4 is divisible by 1 and 2, and 6 is divisible by 1 2 3.

Prime numbers between 1 and 7: 2 3 5 7

Hence, Prime numbers are 2 3 5 and 7 in the given range (1, 7).

Logic to print all prime numbers in a given range

  • Take two values as firstNum and lastNum as a range.
  • It uses a loop (while or for loop) to iterate from firstNum to lastNum.
  • Inside the loop, it initializes a boolean variable isPrime to true, assuming each number is prime initially.
  • Using an if statement, check if firstNum is not equal to 1. If true, enters a nested loop (while or for loop), starts with i=2 and iterates until i<firstNum.
  • Using an if statement, check if firstNum%i==0. If this condition true, sets isPrime to false and and breaks the loop.
  • If firstNum is equal to 1, sets isPrime to false since 1 is not considered a prime number.
  • After completing the inner loop, if isPrime remains true, print firstNum as a prime number.
  • Increment the firstNum after every iteration. After completing the outer loop, close the scanner class.

Java program to print all prime numbers in a given range

Program 1: Using while loop

In the below program we are using while loop to print all prime numbers in a given range.

import java.util.Scanner;
public class PrimeNumberInRange {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter first number");
		int firstNum=s.nextInt();
		System.out.println("Enter second number");
		int lastNum=s.nextInt();
		while(firstNum<=lastNum) {
		boolean isPrime= true;
		if(firstNum!=1) {
			int i=2;
			while(i<firstNum) {
				if(firstNum%i==0) {
					isPrime=false;
					break;
				}                                             
				i++;
			}
		}
		else
			isPrime =false;
		if(isPrime)
		  System.out.println(firstNum);
		firstNum++;
		s.close();
	}
		}
}

Output:

Enter first number
1
Enter second number
5
2
3
5

Java program explanation to print all prime numbers in a given range

  1. The program asks the user to enter two numbers, firstNum and lastNum, defining a range.
  2. It uses a while loop to iterate through each number from firstNum to lastNum.
  3. For each number, it initializes isPrime to true and checks if the number is greater than 1.
  4. If the number is greater than 1, it checks divisibility from 2 up to one less than the number.
  5. And, if any divisor is found, it sets isPrime to false and breaks the loop.
  6. If isPrime remains true after the loop, it prints the number as it is a prime number.
  7. Finally, it increments firstNum and closes the scanner after completing the loop.

Program 2: Using for loop

In the below program we are using for loop to print all prime numbers in a given range.

import java.util.Scanner;
public class PrimeNumberInRange {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter first number");
		int firstNum = s.nextInt();
		System.out.println("Enter second number");
		int lastNum = s.nextInt();
		for (; firstNum <= lastNum;firstNum++) {
			boolean isPrime = true;
			if (firstNum!=1 ) {
				int i = 2;
				for (; i < firstNum; i++) {
					if (firstNum % i == 0) {
						isPrime = false;
						break;
					}
				}
			}
			else
				isPrime = false;
			if (isPrime)
				System.out.println(firstNum);	
			s.close();
		}
	}
}

Output:

Enter first number
50
Enter second number
65
53
59

Java program explanation to print all prime numbers in a given range

  1. The Java program ask the user to enter two numbers: firstNum and lastNum.
  2. Iterate from firstNum to lastNum using a for loop.
  3. For each number in the range:
  4. Assume it is prime by setting isPrime to true.
  5. If the number is 1, set isPrime to false (since 1 is not prime).
  6. Otherwise, check for factors from 2 to number-1:
    • If a factor is found, set isPrime to false and break out of the loop.
  7. If isPrime is still true, print the number as it is a prime number.
  8. Increment firstNum after each iteration.
  9. Close the scanner after the loop ends (though it should be outside the loop).

Conclusion

In this article, we have learnt to print all the prime numbers in a given range in Java. We have understood the logic and moved on implementing the programs. Hope you find this article helped you in understanding the program.

You can also check our another awesome tutorials

  1. Sum of prime numbers in a given range in Java
  2. Java program to find prime factors of a number
  3. Program to finds strong numbers in a range in Java
  4. Print sum of digits of a number in Java

Happy Coding!!

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.