This is a Java Program to Find Prime Numbers Within a Range of n1 and n2.

Enter the upper and lower limits as input. Now we use modulus operation along with double for loops and if-else conditions to get the output.

Here is the source code of the Java Program to Find Prime Numbers Within a Range of n1 and n2. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import java.util.Scanner; public class Prime { public static void main(String args[]) { int s1, s2, s3, flag = 0, i, j; Scanner s = new Scanner(System.in); System.out.println ("Enter the lower limit :"); s1 = s.nextInt(); System.out.println ("Enter the upper limit :"); s2 = s.nextInt(); System.out.println ("The prime numbers in between the entered limits are :"); for(i = s1; i <= s2; i++) { for( j = 2; j < i; j++) { if(i % j == 0) { flag = 0; break; } else { flag = 1; } } if(flag == 1) { System.out.println(i); } } } }

Output:

$ javac Prime.java $ java Prime Enter the lower limit : 2 Enter the upper limit : 20 The prime numbers in between the entered limits are : 3 5 7 11 13 17 19

**Sanfoundry Global Education & Learning Series – 1000 Java Programs.**

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.