QN014. Write a program to print prime numbers between n1 to n2.


1. Objective

To print prime numbers between two given numbers.

2. Theory

In this program we ask for the user to input two numbers to generate list of numbers. For this we use Java Scanner class to store input data and for loop to generate numbers. If statement with some condition is applied to check for the number is prime or not.

3. Source Code
import java.util.Scanner;
public class Qn014{
public static void main(String[] args){
int number;
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
int n1 = input.nextInt();
System.out.print("Enter second number: ");
int n2 = input.nextInt();
for(number=n1; number<=n2; number++)
{
int count=0;
for(int i=2; i<=number/2; i++)
{
if(number%i==0)
{
count++;
break;
}
}
if(count == 0 && number != 1)
System.out.print(number + "\t");
}
}
}
4. Output

Enter first number: 100
Enter second number: 140
101 103 107 113 127 131 137 139

5. Conclusion

In this way, I would like to conclude that, we can find prime number between any two numbers using this program.