# C++ Program to Print Prime numbers in a given range ## Program to find Prime Numbers in a given range

Here we will discuss how to find prime numbers in the range specified by the user using C++ programming language.

Prime numbers are the numbers which have 2 divisors only i.e. the number that can be divided by 1 and the number itself then the number is a prime number.

Example: 2, 3, 5, 7, 11, 13………etc.

In this program, the user will specify a range and we will check for every number in the range for being prime

## Algorithm:-

1. Take two inputs from user (one for the lower limit & other for the upper limit).
2. The inputs are stored in two int type variables say lowerLimit, upperLimit.
3.  Start a loop that will run from lowerLimit till upperLimit with every increment equal to 1
1. For every value call a function prime() with that value as a parameter
2. prime() will tell whether a number is prime or not
1. count Number of divisors of the given number in range 1 to given number
2. If the number of divisors is equal to 2 then the number is a prime number
1. display the number
3. Otherwise the number is not a prime number.
4. All the prime numbers between lowerLimit and upperLimit are printed. #### C++ Code

```#include<iostream>
using namespace std;
//function to chek for prime number
void prime(int num)
{
int div=0;
//checking for number of divisor
for(int i=1;i<=num;i++)
{
if(num%i==0)
div++;
}
//no divisors other than 1 and itself
if(div==2)
cout<<num<<endl;
}
int main()
{
cout<<"Enter range: ";
int lowerLimit, upperLimit;
//user input

cin>>lowerLimit>>upperLimit;
cout<<"Prime numbers between "<<lowerLimit<<" and "<<upperLimit<<" are:"<<endl;

//finding prime numbers in the given range
for(int i=lowerLimit;i<=upperLimit;i++)
prime(i);
return 0;
}
```

#### Output

```Enter range: 1
20
Prime numbers between 1 and 20 are:
2
3
5
7
11
13
17
19
``` 