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

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...