# Java program to print prime numbers in a given range

Let’s learn java program to print prime numbers in a given range.

## Java program to print prime numbers in a given range

To print prime numbers in a given range first user enters starting and ending range using nextInt() method of Scanner class.

In the next step using for loop find prime numbers between starting and ending range. Then we all know the logic from previous post to check if number is prime or not between given range or particular range and finally print the output.

```import java.util.Scanner;
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int startNumber = sc.nextInt();
int endNumber = sc.nextInt();
System.out.println("Prime numbers between range " + startNumber + " and " + endNumber + " are: ");
int counter;
for(int a = startNumber ; a <= endNumber ; a++)
{
// here's the logic to check if number is prime or not
counter = 0;
for(int b = 1 ; b <= a ; b++)
{
if(a % b == 0)
counter = counter + 1;
}
if(counter == 2)
System.out.println(a);
}
sc.close();
}
}
```

Output:

Prime numbers between range 10 and 50 are:
11
13
17
19
23
29
31
37
41
43
47

Also read – Strings in java