C++ Program to find sum of even and odd elements in array using function


In this program, You will learn how to find the sum of even and odd elements in an array using the function in C++.


List is: 2 3 22 11 44

Even sum is: 68

Odd sum is: 14


Example: How to find sum of even and odd elements in array using function in C++

Run it Now
#include<iostream>
using namespace std;

void findSum(int arr[], int n) {

    int se = 0, sod = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] % 2 == 0) {
            se = se + arr[i];
        } else {
            sod = sod + arr[i];
        }
    }
    cout << "\nSum of even array elemenst is :" << se;
    cout << "\nSum of odd array elemenst is :" << sod;
}

int main() {

    int arr[10], i, n;

    cout << "Enter Size of an Array :";
    cin>>n;

    cout << "\nEnter Array elemensts :";
    for (i = 0; i < n; i++) {
        cin >> arr[i];
    }

    findSum(arr, n);

    return 0;
}

Output:

Enter Size of an Array :5
Enter Array elemensts :2 3 22 11 44
Sum of even array elemenst is :68
Sum of odd array elemenst is :14