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:
- We declare an integer array
numbers[]
containing ten elements. - The size of the array is determined using
sizeof(numbers) / sizeof(numbers[0])
. - We initialize two variables
sumEven
andsumOdd
to store the sum of even and odd numbers respectively. - Using a
for
loop, we iterate over the array elements. - Inside the loop, we check if the number is even using
number[i] % 2 == 0
and add it tosumEven
. - If the number is odd, we add it to
sumOdd
. - 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:
- We declare an integer array
numbers[]
with ten elements. - We determine the size of the array using
sizeof()
. - Two integer variables,
sumEven
andsumOdd
, are initialized to store the sums. - A
while
loop starts withi = 0
and runs whilei < size
. - Inside the loop, we check whether the element is even or odd and add it to the respective sum.
- We increment
i
to move to the next element. Reference: C Increment Operator - 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:
- A
for
loop: Iterating over the array and checking each element. - A
while
loop: Using a different loop structure to achieve the same goal.