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
#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;
}
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