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
- Java programming basics
- Java Operators
- Loop Concepts
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
- The program asks the user to enter two numbers,
firstNum
andlastNum
, defining a range. - It uses a
while
loop to iterate through each number fromfirstNum
tolastNum
. - For each number, it initializes
isPrime
totrue
and checks if the number is greater than1
. - If the number is greater than
1
, it checks divisibility from2
up to one less than the number. - And, if any divisor is found, it sets
isPrime
tofalse
and breaks the loop. - If
isPrime
remainstrue
after the loop, it prints the number as it is a prime number. - 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
- The Java program ask the user to enter two numbers:
firstNum
andlastNum
. - Iterate from
firstNum
tolastNum
using afor
loop. - For each number in the range:
- Assume it is prime by setting
isPrime
totrue
. - If the number is
1
, setisPrime
tofalse
(since1
is not prime). - Otherwise, check for factors from
2
tonumber-1
:- If a factor is found, set
isPrime
tofalse
and break out of the loop.
- If a factor is found, set
- If
isPrime
is stilltrue
, print the number as it is a prime number. - Increment
firstNum
after each iteration. - 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
- Sum of prime numbers in a given range in Java
- Java program to find prime factors of a number
- Program to finds strong numbers in a range in Java
- Print sum of digits of a number in Java
Happy Coding!!