forked from codehouseindia/Everything
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Prime-number
51 lines (40 loc) · 984 Bytes
/
Prime-number
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
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;
int checkPrimeNumber(int);
int main() {
int n1, n2;
bool flag;
cout << "Enter two positive integers: ";
cin >> n1 >> n2;
// swapping n1 and n2 if n1 is greater than n2
if (n1 > n2) {
n2 = n1 + n2;
n1 = n2 - n1;
n2 = n2 - n1;
}
cout << "Prime numbers between " << n1 << " and " << n2 << " are: ";
for(int i = n1+1; i < n2; ++i) {
// If i is a prime number, flag will be equal to 1
flag = checkPrimeNumber(i);
if(flag)
cout << i << " ";
}
return 0;
}
// user-defined function to check prime number
int checkPrimeNumber(int n) {
bool isPrime = true;
// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
isPrime = false;
}
else {
for(int j = 2; j <= n/2; ++j) {
if (n%j == 0) {
isPrime = false;
break;
}
}
}
return isPrime;
}