Given a list of integers, separate them into two groups — even numbers (divisible by 2) and odd numbers (not divisible by 2) — and report the sum and count of each group. This is a classic array-scanning problem that demonstrates the modulo operator, accumulator variables, and loop-based input. The original post had no code; this rewrite provides a complete, working C89-compliant implementation.
How It Works
- Read N integers into an array.
- For each element, test
a[i] % 2 == 0. - Add to
even_sum(and incrementeven_count) or toodd_sum(and incrementodd_count). - Print both sums and counts.
| i | a[i] | a[i] % 2 | Group | even_sum | odd_sum |
|---|---|---|---|---|---|
| 0 | 1 | 1 | Odd | 0 | 1 |
| 1 | 2 | 0 | Even | 2 | 1 |
| 2 | 3 | 1 | Odd | 2 | 4 |
| 3 | 4 | 0 | Even | 6 | 4 |
| 4 | 5 | 1 | Odd | 6 | 9 |
Result for [1,2,3,4,5]: even_sum=6, odd_sum=9 ✓
C Program: Sum of Even and Odd Numbers
/* Read N integers and print the sum of even and odd numbers separately
* Compile: gcc -ansi -Wall -Wextra sumeodd.c -o sumeodd */
#include <stdio.h>
#define MAX 100
int main(void)
{
int a[MAX], n, i;
int even_sum = 0, odd_sum = 0;
int even_count = 0, odd_count = 0;
printf("How many integers? ");
if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
printf("Enter a number between 1 and %d.\n", MAX);
return 1;
}
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
if (scanf("%d", &a[i]) != 1) {
printf("Invalid input.\n");
return 1;
}
}
for (i = 0; i < n; i++) {
if (a[i] % 2 == 0) {
even_sum += a[i];
even_count++;
} else {
odd_sum += a[i];
odd_count++;
}
}
printf("\nEven numbers (%d): sum = %d\n", even_count, even_sum);
printf("Odd numbers (%d): sum = %d\n", odd_count, odd_sum);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra sumeodd.c -o sumeodd
./sumeodd
Sample Output
How many integers? 10 Enter 10 integers: 1 2 3 4 5 6 7 8 9 10 Even numbers (5): sum = 30 Odd numbers (5): sum = 25
How many integers? 5 Enter 5 integers: 3 7 2 9 4 Even numbers (2): sum = 6 Odd numbers (3): sum = 19
Code Explanation
- Two accumulators, two counters —
even_sum/odd_sumaccumulate the running totals;even_count/odd_counttrack how many of each kind there are. Initializing both to 0 at declaration is important — uninitialized local variables hold garbage in C. - Single-pass loop — the array is read in one loop and classified in a second loop. These could be merged into one loop (reading and classifying in the same iteration), but splitting them makes the logic clearer. For this problem, either approach is correct.
- Bounds validation — checking
n < 1 || n > MAXafter reading prevents array overflow. Without this, entering n=200 with a MAX=100 array causes undefined behavior (writing past the array boundary). - #define MAX 100 — using a named constant instead of a magic number means changing the array size requires editing exactly one line. Always prefer named constants over magic numbers in production code.
What This Program Teaches
- Accumulator pattern — initializing a variable to zero before a loop and adding to it inside the loop is the canonical way to compute sums and counts in C. It appears in averaging, totalling invoices, computing checksums, and signal integration.
- Modulo for classification —
% 2partitions integers into exactly two groups. The same technique partitions into N groups with% N. Classic uses: round-robin scheduling, hash-bucket assignment, striped array access. - Input validation before use — check both the return value of
scanfand the logical validity of the read value (range check). Reading values first, validating second, is the standard idiom.
Related Programs
- Odd or Even Check in C
- Sum of N Natural Numbers in C
- Sum of Integers Divisible by 7 in C
- Array Summation Using Pointers in C
- Linear Search in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.