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

#include <stdio.h>

int isPrime(int num) {
    if(num <= 1) return 0;
    for(int i = 2; i < num; i++) {
        if(num % i == 0)
            return 0;
    }
    return 1;
}

int main() {
    int num, found = 0;
    printf("Enter a positive integer: ");
    scanf("%d", &num);

    for(int i = 2; i <= num/2; i++) {
        if(isPrime(i) && isPrime(num - i)) {
            printf("%d = %d + %d\n", num, i, num - i);
            found = 1;
            break;
        }
    }

    if(!found)
        printf("%d cannot be expressed as the sum of two primes.\n", num);

    return 0;
}
Enter a positive integer: 34
34 = 3 + 31
Sai Kishore Randhi, author
About the author

Sai Kishore Randhi

Embedded systems, VLSI, and chip design

Sai Kishore writes about embedded systems, VLSI, chip design, and the programming concepts behind modern hardware.

View LinkedIn profile ↗