-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathQ22_Primenumberslist.cpp
39 lines (37 loc) · 977 Bytes
/
Q22_Primenumberslist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// C++ program to find the prime numbers between a given interval
#include<iostream>
using namespace std;
// Function to print prime number in given range
void primeInRange(int L, int U)
{
int flag;
// Traverse each number in the interval with the help of for loop
for (int i=L;i<=U;i++) {
// Skip 0 and 1 as they are neither prime nor composite
if (i == 1 || i == 0)
continue;
// flag variable to tell if i is prime or not
flag = 1;
for (int j=2;j<=i/2;j++) {
if (i % j == 0) {
flag=0;
break;
}
}
// flag = 1 means i is prime
// and flag = 0 means i is not prime
if (flag == 1)
cout << i << " ";
}
}
// Driver Code
int main()
{
// Given Range
int L,U;
cout<<"Enter the Range:";
cin>>L>>U;
cout<<"The list of prime numbers in given range:";
// Function Call
primeInRange(L,U);
}