# C++ program to find the sum of even and odd numbers of the array using class

Given an array of integers, we have to find the sum of even and odd numbers of the array using the class and object approach.
Submitted by Shubh Pachori, on September 14, 2022

Example:

```Input:
: 8
: 4
: 3
: 5
: 8
: 0
: 9
: 6
: 1
: 7

Output:
Sum of all even number : 26
Sum of all odd number : 25
```

## C++ code to find the sum of even and odd numbers of the array using the class and object approach

```#include <iostream>
using namespace std;

// create a class
class Array {
// private data member
private:
int arr;

// public member functions
public:
// getArray() function to insert
// array elements
void getArray() {
for (int index = 0; index < 10; index++) {
cout << "[" << index << "]: ";
cin >> arr[index];
}
}

// sumEvenOdd() function to find sum of even
// and odd numbers in the array
void sumEvenOdd() {
// initialising int type variables to
// perform operations
int index, sumeven = 0, sumodd = 0;

// for loop to traverse the whole array
for (index = 0; index < 10; index++) {
// if condition to find sum of
// the even numbers of the array
if (arr[index] % 2 == 0) {
sumeven = sumeven + arr[index];
}

// else condition to find sum of
// the odd numbers of the array
else {
sumodd = sumodd + arr[index];
}
}

cout << "\nSum of all even number : " << sumeven << endl;
cout << "Sum of all odd number : " << sumodd << endl;
}
};

int main() {
// create a object
Array A;

// calling getArray() function to
// insert the array
A.getArray();

// calling sumEvenOdd() function to find
// sum of even and odd numbers in the array
A.sumEvenOdd();

return 0;
}
```

Output:

```: 1
: 2
: 3
: 4
: 5
: 6
: 7
: 8
: 9
: 10

Sum of all even number : 30
Sum of all odd number : 25
```

Explanation:

In the above code, we have created a class Array, one int type array data members arr to store the elements of the array, and public member functions getArray() and sumEvenOdd() to store the array elements and to find sum of even and odd numbers of the array.

In the main() function, we are creating an object A of class Array, reading the inputted array by the user using getArray() function, and finally calling the sumEvenOdd() member function to find sum of even and odd numbers of the array. The sumEvenOdd() function contains the logic to find sum of even and odd numbers of the array and printing the result.

What's New (MCQs)

Top Interview Coding Problems/Challenges!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.