CategoryC Program

C Program to find the sum of all even numbers in an array

Learn how to find the sum of all even numbers stored in a C array using the modulo operator, with validated code, examples, and a dry run.

To find the sum of all even numbers in an array, examine every element and add it to a running total only when it is exactly divisible by 2.

The condition for an even integer in C is:

number % 2 == 0

For example, the even elements in 7, -4, 0, 13, 18, 22 are -4, 0, 18, and 22. Their sum is:

-4 + 0 + 18 + 22 = 36

C Program to Find the Sum of Even Array Elements

#include <stdio.h>

#define MAX_SIZE 100

int main(void) {
    int array[MAX_SIZE];
    int size;
    int foundEven = 0;
    long long evenSum = 0;

    printf("Enter the number of elements: ");
    if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
        printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
        return 1;
    }

    printf("Enter %d elements:\n", size);
    for (int index = 0; index < size; index++) {
        if (scanf("%d", &array[index]) != 1) {
            printf("Invalid array element.\n");
            return 1;
        }
    }

    for (int index = 0; index < size; index++) {
        if (array[index] % 2 == 0) {
            evenSum += array[index];
            foundEven = 1;
        }
    }

    if (!foundEven) {
        printf("No even elements were found.\n");
    }

    printf("Sum of even elements = %lld\n", evenSum);

    return 0;
}

Sample Output

Enter the number of elements: 6
Enter 6 elements:
7 -4 0 13 18 22
Sum of even elements = 36

The input values can be entered on one line or on separate lines because scanf() treats whitespace as a separator.

How the Program Works

  1. evenSum starts at 0, which is the identity value for addition.
  2. foundEven starts at 0 and records whether the array contains at least one even element.
  3. The first loop reads the array elements.
  4. The second loop tests each element using array[index] % 2 == 0.
  5. When the condition is true, the element is added to evenSum, and foundEven becomes 1.
  6. After every element has been checked, the program prints the final sum.

Here is a dry run for the sample array:

ElementEven?CalculationRunning sum
7NoSkip0
-4Yes0 + (-4)-4
0Yes-4 + 0-4
13NoSkip-4
18Yes-4 + 1814
22Yes14 + 2236

The array itself is not modified; the program only reads each value and updates the separate running total.

Why Use long long for the Sum?

The array stores int values, but adding many large integers can produce a total outside the range of an int. Using long long gives the running sum a wider range on common C implementations.

The %lld format specifier is used because evenSum has type long long.

Zero and Negative Even Numbers

Zero is even because 0 % 2 is 0. Adding zero does not change the total, but it still counts as an even element.

Negative even numbers also satisfy the same condition and must be included:

-8 % 2 = 0

If an array contains -8, 3, 4, the sum of its even elements is -8 + 4 = -4.

What If There Are No Even Elements?

If the array contains only odd numbers, evenSum remains 0 and foundEven remains false:

Enter the number of elements: 4
Enter 4 elements:
3 7 -5 11
No even elements were found.
Sum of even elements = 0

The flag distinguishes this case from an array whose even elements genuinely add up to zero, such as -4, 4, 7.

Can the Sum Be Calculated While Reading Input?

Yes. The modulo test and addition can be placed immediately after each successful scanf() call. That combines input and processing in one loop.

The two-loop version shown above keeps input separate from calculation, which is easier to read and leaves the complete array available for other operations. Both approaches have O(n) time complexity.

Time and Space Complexity

  • Time complexity: O(n), because all n array elements are tested once. Input also takes O(n), so the complete program remains linear.
  • Extra space complexity: O(1) for the summing operation because it uses only a running total, a flag, and a loop index. The input array occupies O(n) space.

Common Mistakes

  • Adding every element instead of adding only elements that satisfy % 2 == 0.
  • Using / instead of % to test divisibility.
  • Forgetting to initialize evenSum to 0.
  • Excluding zero even though it is evenly divisible by 2.
  • Ignoring negative even values.
  • Printing a long long value with %d instead of %lld.

By combining a modulo check with a running total, the program calculates the sum of all even array elements in a single traversal.