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


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


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

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

s_even = s_even +arr [i] } }


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

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

int main() {

    int arr[5], i, seven = 0, sodd = 0;

    cout << "Enter any 5 numbers :";
    for (i = 0; i < 5; i++) {
        cin >> arr[i];
    }

    for (i = 0; i < 5; i++) {
        if (arr[i] % 2 == 0) {
            seven = seven + arr[i];
        } else {
            sodd = sodd + arr[i];
        }
    }

    cout << "\nSum of Even :" << seven;
    cout << "\nSum of Odd :" << sodd;

    return 0;
}

Output:

Enter any 5 numbers :1 2 3 4 5
Sum of Even :6
Sum of Odd :9