forked from atul14357/Hacktoberfest1435
-
Notifications
You must be signed in to change notification settings - Fork 0
/
two sum check
39 lines (30 loc) · 847 Bytes
/
two sum check
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
public class Main {
public static void main(String[] args) {
int number = 34;
boolean flag = false;
for (int i = 2; i <= number / 2; ++i) {
// condition for i to be a prime number
if (checkPrime(i)) {
// condition for n-i to be a prime number
if (checkPrime(number - i)) {
// n = primeNumber1 + primeNumber2
System.out.printf("%d = %d + %d\n", number, i, number - i);
flag = true;
}
}
}
if (!flag)
System.out.println(number + " cannot be expressed as the sum of two prime numbers.");
}
// Function to check prime number
static boolean checkPrime(int num) {
boolean isPrime = true;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
isPrime = false;
break;
}
}
return isPrime;
}
}