forked from hite5h/primeno.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimesum.cpp
More file actions
48 lines (39 loc) · 767 Bytes
/
primesum.cpp
File metadata and controls
48 lines (39 loc) · 767 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
48
#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;
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0)
{
isPrime = false;
break;
}
}
return isPrime;
}