C Program to Check Sum of Two Prime Numbers
C Program to Check Sum of Two Prime Numbers
A number challenge asks for all unordered prime pairs that add to a target, without printing each pair twice in opposite orders.
Test a prime first addend and the complementary addend target−first; limiting first to target/2 avoids reversed duplicate pairs.
Before this exercise, review loops and stopping conditions and functions and parameters. The worked program below applies those ideas to this specific problem.
Explain
Explain why sum of two primes uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use target three and report the absence of a valid pair.
Sum of Two Primes: follow the data
Given data
A number challenge asks for all unordered prime pairs that add to a target, without printing each pair twice in opposite orders.
Rule
Test a prime first addend and the complementary addend target−first; limiting first to target/2 avoids reversed duplicate pairs.
Observed result
3 + 7 = 10 5 + 5 = 10
Understand the operation
The first addend begins at two, the smallest prime. Both addends must independently satisfy the prime helper. A repeated pair such as 5+5 is valid when repeated addends are allowed.
This program checks a finite small target. It does not present the claim that every even integer above two has such a representation as a proven theorem.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
static int is_prime(int value) {
if (value < 2) return 0;
for (int divisor = 2; divisor <= value / divisor; divisor++) {
if (value % divisor == 0) return 0;
}
return 1;
}
int main(void) {
int number = 10, found = 0;
for (int first = 2; first <= number / 2; first++) {
if (is_prime(first) && is_prime(number - first)) { printf("%d + %d = %d\n", first, number - first, number); found = 1; }
}
if (!found) puts("No prime pair");
return 0;
}
Expected output
3 + 7 = 10
5 + 5 = 10
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
static int is_prime(int value) {
if (value < 2) return 0;
for (int divisor = 2; divisor <= value / divisor; divisor++) {
if (value % divisor == 0) return 0;
}
return 1;
}
int main(void) {
int number = 10, found = 0;
for (int first = 2; first <= number / 2; first++) {
if (is_prime(first) && is_prime(number - first)) { printf("%d + %d = %d\n", first, number - first, number); found = 1; }
}
if (!found) puts("No prime pair");
return 0;
}
For target 10, each first addend determines exactly one complementary value.
First three pairs with seven; first five pairs with five. The reverse seven-plus-three pair is not emitted.
Follow the worked calculation
Start with the sample
For target 10, each first addend determines exactly one complementary value.
Apply the rule
First three pairs with seven; first five pairs with five. The reverse seven-plus-three pair is not emitted.
Check the result
3 + 7 = 10 5 + 5 = 10
3 + 7 = 10 5 + 5 = 10
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
static int is_prime(int value) {
if (value < 2) return 0;
for (int divisor = 2; divisor <= value / divisor; divisor++) {
if (value % divisor == 0) return 0;
}
return 1;
}
int main(void) {
int number = 10, found = 0;
for (int first = 2; first <= number / 2; first++) {
if (is_prime(first) && is_prime(number - first)) { printf("%d + %d = %d\n", first, number - first, number); found = 1; }
}
if (!found) puts("No prime pair");
return 0;
}
Use target three and report the absence of a valid pair.
Worked answer
There is no way to express three as a sum of two primes at least two. The found flag remains zero and the program prints No prime pair.
C17 · complete program
#include <stdio.h>
static int is_prime(int value) {
if (value < 2) return 0;
for (int divisor = 2; divisor <= value / divisor; divisor++) {
if (value % divisor == 0) return 0;
}
return 1;
}
int main(void) {
int number = 3, found = 0;
for (int first = 2; first <= number / 2; first++) {
if (is_prime(first) && is_prime(number - first)) { printf("%d + %d = %d\n", first, number - first, number); found = 1; }
}
if (!found) puts("No prime pair");
return 0;
}
Expected output
No prime pair
Run in C compiler →Recognize the important boundaries
The exercise defines unordered pairs and permits the same prime twice. Change both bounds and explanations if the problem asks for ordered or distinct addends.
Trial division is appropriate for these small targets. Precomputing prime membership can make many large repeated queries more efficient.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Test a prime first addend and the complementary addend target−first; limiting first to target/2 avoids reversed duplicate pairs.
Continue with the prime-test helper.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.