C Program to Find Sum of Even & Add Numbers in Array

Question: Write a program in C to read an array containing n elements and find sum of all odd numbers and even numbers.

Program


#include<stdio.h>

int main()
{
 int arr[100], i, n, esum=0, osum=0;
 printf("Enter n:");
 scanf("%d", &n);
 
 /* Reading Array */
 printf("Enter numbers:\n");
 for(i=0;i< n;i++)
 {
  printf("arr[%d]=",i);
  scanf("%d",&arr[i]);
 }
 /* Calculating even and odd count */
 for(i=0;i< n;i++)
 {
  if(arr[i]%2==0)
  {
   esum = esum + arr[i];
  }
  else
  {
   osum = osum + arr[i];
  }
 }
 /* Displaying result */
 printf("Even sum = %d\n",esum);
 printf("Odd sum = %d", osum);

 return(0);
}

Output

Enter n: 4↲
Enter numbers:
arr[0] = 21↲
arr[1] = 11↲
arr[2] = 13↲
arr[3] = 12↲
Even sum = 12
Odd sum = 45