In this post, we will learn **how to check whether a number can be expressed as the sum of two prime numbers using C** Programming language.

For example: If we take the number 34, it can be expressed as sum of two prime numbers in the following ways:

- Sum of 3 and 31
- Sum of 5 and 29
- Sum of 11 and 23
- Sum of 17 and 17

If the number can be expressed as sum of two prime numbers, then this program will show all the possible combination of prime numbers.

So, without further ado, letâ€™s begin this tutorial.

**C Program To Check Whether a Number can be Expressed as Sum of Two Prime Numbers**

// C Program To Check Whether a Number can be Expressed as Sum of Two Prime Numbers #include <stdio.h> int sum(int n); int main(){ int i, num, flag = 0; // Asking for Input printf("Enter a number: "); scanf("%d", &num); for (i = 2; i <= num / 2; ++i){ if (sum(i) == 1){ if (sum(num - i) == 1){ printf("%d can be expressed as sum of %d and %d.\n", num, i, num - i); flag = 1; } } } if (flag == 0){ printf("%d cannot be expressed as sum of two prime numbers."); return 0; } } // To Check Prime Number int sum(int n){ int i, isPrime = 1; for (i = 2; i <= n / 2; ++i){ if (n % i == 0){ isPrime = 0; break; } } return isPrime; }

**Output**

```
Enter a number: 34
34 can be expressed as sum of 3 and 31.
34 can be expressed as sum of 5 and 29.
34 can be expressed as sum of 11 and 23.
34 can be expressed as sum of 17 and 17.
```

**How Does This Program Work ?**

**//check if a number is prime or not
int sum(int n){
int i, isPrime = 1;
for(i = 2; i <= n/2; ++i){
if(n % i == 0){
isPrime = 0;
break;
}
}
return isPrime;
}**

In this program, we have declared a custom function which will check whether a number is **prime** or **not**.

** printf("Enter a number: ");
scanf("%d", &num);**

Then, the user is asked to enter a number. The value of the number gets stored in the **num** named variable.

` `**for(i = 2; i <= num/2; ++i){
if (sum(i) == 1){
if (sum(num-i) == 1){
printf("The given %d can be expressed as the sum of %d and %d.\n", num, i, num - i);
flag = 1;
}
}
}**

For each iteration, we check whether the value of **i** is prime or not. If **num – i **is also prime, then we can express the number as the sum of two prime numbers.

We assigned **flag** a value of **1**, otherwise if it remains **0**, it will display a message that sum that can’t be expressed.

**Conclusion**

I hope after going through this post, you understand **how to check whether a number can be expressed as the sum of two prime numbers using C** Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

**Also Read:**