C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Arrays in C

Sum of Even and Odd Elements in Array

C Program: Sum of Even and Odd Elements in Array

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i;

    int evenSum = 0, oddSum = 0;

 

    // Input size of the array

    printf("Enter number of elements in the array: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    for (i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // Calculate sum of even and odd elements

    for (i = 0; i < n; i++) {

        if (arr[i] % 2 == 0)

            evenSum += arr[i];

        else

            oddSum += arr[i];

    }

 

    // Display results

    printf("\nSum of even elements: %d", evenSum);

    printf("\nSum of odd elements: %d\n", oddSum);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements in the array: 6
Enter 6 elements:
10 15 20 25 30 35

OUTPUT :
Sum of even elements: 60
Sum of odd elements: 75

Explanation

  1. The program reads the size and elements of the array.
  2. Using a loop, it checks if each element is even or odd:
    • Even numbers are added to evenSum.
    • Odd numbers are added to oddSum.
  3. Both sums are displayed at the end.