Requirements:
(1) Write a program that declares a single-dimensional arrays named num.
(2) The numbers to be stored in num are 45, 86, 12, 37, 28, 113.
(3) Sum odds and evens separately.
(4) Finally, give the results that will be displayed on the screen.
#include <stdio.h>
int main()
{
int num[] = { 45, 86, 12, 37, 28, 113 };
int size = sizeof(num) / sizeof(num[0]);
int sumOdds = 0;
int sumEvens = 0;
int i;
for(i = 0; i < size; ++i)
{
if(num[i] % 2 == 0)
{
sumEvens += num[i];
}
else
{
sumOdds += num[i];
}
}
printf("Sum of odds is %d\nSum of evens is %d\n", sumOdds, sumEvens);
return 0;
}
Comments
Leave a comment