Find the Sum of Even and Odd Elements Separately in an Array in C

To find the sum of even and odd elements separately in an array in C, we iterate through the array, check whether each element is even or odd using the modulus operator %, and accumulate their sums in separate variables. This method efficiently computes both sums in a single pass through the array.


Examples to Find the Sum of Even and Odd Elements

1. Using a for Loop to Find the Sum of Even and Odd Numbers

In this example, we declare an array of integers and use a for loop to iterate through each element. If the element is even, we add it to sumEven, and if it is odd, we add it to sumOdd.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    int sumEven = 0, sumOdd = 0;

    // Iterate through the array and calculate sums
    for (int i = 0; i < size; i++) {
        if (numbers[i] % 2 == 0) {
            sumEven += numbers[i];
        } else {
            sumOdd += numbers[i];
        }
    }

    printf("Sum of even numbers: %d\n", sumEven);
    printf("Sum of odd numbers: %d\n", sumOdd);

    return 0;
}

Explanation:

  1. We declare an integer array numbers[] containing ten elements.
  2. The size of the array is determined using sizeof(numbers) / sizeof(numbers[0]).
  3. We initialize two variables sumEven and sumOdd to store the sum of even and odd numbers respectively.
  4. Using a for loop, we iterate over the array elements.
  5. Inside the loop, we check if the number is even using number[i] % 2 == 0 and add it to sumEven.
  6. If the number is odd, we add it to sumOdd.
  7. Finally, we print the sum of even and odd numbers.

Output:

Sum of even numbers: 30
Sum of odd numbers: 25

2. Using a while Loop to Find the Sum of Even and Odd Numbers

Instead of a for loop, we will use a while loop to traverse the array and compute the sums of even and odd numbers.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int numbers[] = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    int sumEven = 0, sumOdd = 0;
    int i = 0;

    // Iterate through the array using a while loop
    while (i < size) {
        if (numbers[i] % 2 == 0) {
            sumEven += numbers[i];
        } else {
            sumOdd += numbers[i];
        }
        i++;
    }

    printf("Sum of even numbers: %d\n", sumEven);
    printf("Sum of odd numbers: %d\n", sumOdd);

    return 0;
}

Explanation:

  1. We declare an integer array numbers[] with ten elements.
  2. We determine the size of the array using sizeof().
  3. Two integer variables, sumEven and sumOdd, are initialized to store the sums.
  4. A while loop starts with i = 0 and runs while i < size.
  5. Inside the loop, we check whether the element is even or odd and add it to the respective sum.
  6. We increment i to move to the next element. Reference: C Increment Operator
  7. Finally, we print the sums of even and odd numbers.

Output:

Sum of even numbers: 80
Sum of odd numbers: 75

Conclusion

In this tutorial, we learned how to find the sum of even and odd elements separately in an array using:

  1. A for loop: Iterating over the array and checking each element.
  2. A while loop: Using a different loop structure to achieve the same goal.