C++ Program to find the sum of even and odd elements in the array using the 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 the sum is: 68

The odd sum is: 14

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

#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 << "Sum of even array element:" << se;
    cout << "\nSum of odd array element:" << sod;
}

int main() {

    int arr[10], i, n;

    cout << "Enter size of an array:";
    cin>>n;

    cout << "Enter array elements:";
    for (i = 0; i < n; i++) {
        cin >> arr[i];
    }

    findSum(arr, n);

    return 0;
}

Output:

Enter size of an array:5
Enter array elements:2 3 4 5 6
Sum of even array element:12
Sum of odd array element:8

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.