×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

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

Submitted by Shubh Pachori, on September 14, 2022

Problem statement

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.

Example:

Input: 
[0]: 8
[1]: 4
[2]: 3
[3]: 5
[4]: 8
[5]: 0
[6]: 9
[7]: 6
[8]: 1
[9]: 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[10];

  // 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

[0]: 1
[1]: 2
[2]: 3
[3]: 4
[4]: 5
[5]: 6
[6]: 7
[7]: 8
[8]: 9
[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[10] 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.

C++ Class and Object Programs (Set 2) »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.