The program in c to create a matrix age[20] to store any twenty ages and print the sum of all even and odd ages respectively is given below:
#include <stdio.h>
int main() {
int age[20], i, even_sum = 0, odd_sum = 0;
printf("Enter 20 ages: \n");
for (i = 0; i < 20; i++) {
scanf("%d", &age[i]);
}
for (i = 0; i < 20; i++) {
if (age[i] % 2 == 0) {
even_sum += age[i];
} else {
odd_sum += age[i];
}
}
printf("Sum of even ages: %d\n", even_sum);
printf("Sum of odd ages: %d\n", odd_sum);
return 0;
}
Output:
Enter 20 ages:
1
2
3
4
5
6
7
8
9
10
19
18
17
16
15
14
13
12
11
20
Sum of even ages: 110
Sum of odd ages: 100
Pro-Tips💡
This program uses a for loop to read in 20 ages from the user, and another for loop to calculate the sum of even and odd ages.
The program then prints out the sums.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.