An array can store multiple values of the same data type. In this program, the user enters 5 numbers into an integer array. Each array element is checked using the if-else statement. If the number is even, it is added to the even total. Otherwise, it is added to the odd total. Finally, the program displays the sum of even and odd numbers separately.
How It Works?
r[5] and variables k, e, and d.e and d with 0.for loop to accept 5 numbers from the user and store them in the array.for loop to access each array element.r[k] % 2 == 0.e; otherwise add it to d.#include <stdio.h> #include <conio.h> main() { int r[5], k, e = 0, d = 0; clrscr(); for(k = 0; k <= 4; k = k + 1) { printf("\nEnter no : "); scanf("%d", &r[k]); } for(k = 0; k <= 4; k = k + 1) { printf("\n%d", r[k]); if(r[k] % 2 == 0) { e = e + r[k]; } else { d = d + r[k]; } } printf("\nEven total %d", e); printf("\nOdd total %d", d); getch(); }
for loop.for loops, and the if-else statement to separate and add even and odd array elements.6. Initialize two variables even and odd with 0. Check each array element using % 2. If the remainder is 0, add it to the even total; otherwise add it to the odd total.#include <stdio.h> #include <conio.h> main() { int r[6], k, even = 0, odd = 0; clrscr(); for(k = 0; k <= 5; k++) { printf("Enter no : "); scanf("%d", &r[k]); } for(k = 0; k <= 5; k++) { if(r[k] % 2 == 0) { even = even + r[k]; } else { odd = odd + r[k]; } } printf("\nEven total = %d", even); printf("\nOdd total = %d", odd); getch(); }
for loop checks each element using the modulus operator %. Numbers divisible by 2 are added to the even variable, while remaining numbers are added to the odd variable. Finally, both totals are displayed separately.