-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathCheckWhether.cpp
More file actions
47 lines (38 loc) · 867 Bytes
/
CheckWhether.cpp
File metadata and controls
47 lines (38 loc) · 867 Bytes
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
#include <iostream>
using namespace std;
bool checkPrime(int n);
int main() {
int n, i;
bool flag = false;
cout << "Enter a positive integer: ";
cin >> n;
for(i = 2; i <= n/2; ++i) {
if (checkPrime(i)) {
if (checkPrime(n - i)) {
cout << n << " = " << i << " + " << n-i << endl;
flag = true;
}
}
}
if (!flag)
cout << n << " can't be expressed as sum of two prime numbers.";
return 0;
}
// Check prime number
bool checkPrime(int n) {
int i;
bool isPrime = true;
// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
isPrime = false;
}
else {
for(i = 2; i <= n/2; ++i) {
if(n % i == 0) {
isPrime = false;
break;
}
}
}
return isPrime;
}