# Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers

In this program, you’ll learn to check whether a given number can be expressed as a sum of two prime numbers or not. This is done with the help of loops and break statements in Java.

To accomplish this task, `checkPrime()` function is created.

The `checkPrime()` returns 1 if the number passed to the function is a prime number.

## Example: Integer as a Sum of Two Prime Numbers

1. `public class CheckPrime {`
2. ` public static void main(String[] args) {`
3. ` int number = 34;`
4. ` boolean flag = false;`
5. ` for (int i = 2; i <= number / 2; ++i) {`
6. ` // condition for i to be a prime number`
7. ` if (checkPrime(i)) {`
8. ` // condition for n-i to be a prime number`
9. ` if (checkPrime(number - i)) {`
10. ` // n = primeNumber1 + primeNumber2`
11. ` System.out.printf("%d = %d + %d\n", number, i, number - i);`
12. ` flag = true;`
13. ` }`
14. ` }`
15. ` }`
16. ` if (!flag)`
17. ` System.out.println(number + " cannot be expressed as the sum of two prime numbers.");`
18. ` }`
19. ` // Function to check prime number`
20. ` static boolean checkPrime(int num) {`
21. ` boolean isPrime = true;`
22. ` for (int i = 2; i <= num / 2; ++i) {`
23. ` if (num % i == 0) {`
24. ` isPrime = false;`
25. ` break;`
26. ` }`
27. ` }`
28. ` return isPrime;`
29. ` }`
30. `}`

When you run the program, the output will be:

```34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17```