×

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 product 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 product of even and odd numbers of the array using the class and object approach.

Example:

Input: 
[0]: 8
[1]: 7
[2]: 6
[3]: 5
[4]: 6
[5]: 7
[6]: 9
[7]: 4
[8]: 23
[9]: 4

Output: 
Product of all even number : 9216
Product of all odd number : 2205

C++ code to find the product 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];
    }
  }

  // productEvenOdd() function to find product of even
  // and odd numbers in the array
  void productEvenOdd() {
    // initialising int type variables to perform operations
    int index, producteven = 1, productodd = 1;

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

      // else condition to find product of
      // the odd numbers of the array
      else {
        productodd = productodd * arr[index];
      }
    }

    cout << "\nProduct of all even number : " << producteven << endl;
    cout << "Product of all odd number : " << productodd << endl;
  }
};

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

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

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

  return 0;
}

Output

[0]: 1
[1]: 2
[2]: 3
[3]: 4
[4]: 5
[5]: 6
[6]: 7
[7]: 8
[8]: 9
[9]: 10

Product of all even number : 3840
Product of all odd number : 945

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 productEvenOdd() to store the array elements and to find product 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 productEvenOdd() member function to find product of even and odd numbers of the array. The productEvenOdd() function contains the logic to find product 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.