×

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 print the product of all odd and even numbers in a range using class

Submitted by Shubh Pachori, on September 04, 2022

Problem statement

Given a range, we have to print the product of all odd and even numbers in that range using the class and object approach.

Example:

Input: 
Enter Range : 1 10

Output: 
Product of all even numbers is 3840
Product of all odd numbers is 945

C++ code to print the product of all odd and even numbers in a range using the class and object approach

#include <iostream>
using namespace std;

// create a class
class OddEven {
  // private data members
 private:
  int start, end;

  // public member function
 public:
  // getRange() function to enter range
  void getRange() {
    cout << "Enter Range : ";
    cin >> start >> end;
  }

  // productOddEven() function to multiply 
  // all odd and even numbers
  void productOddEven() {
    // initializing int type variables to 
    // perform operations
    int index, product_even = 1, product_odd = 1;

    // for loop to traverse all numbers in the range
    for (index = start; index <= end; index++) {
      // condition to check even numbers
      if (index % 2 == 0) {
        // adding all even numbers in the range
        product_even = product_even * index;
      }

      // condition for odd numbers
      else {
        // adding all odd numbers in the range
        product_odd = product_odd * index;
      }
    }

    cout << "Product of all even numbers is " << product_even << "\n"
         << "Product of all odd numbers is " << product_odd << endl;
  }
};

int main() {
  // create a object
  OddEven O;

  // calling getRange() function to get range
  O.getRange();

  // productOddEven() function to find product 
  // of all even and odd numbers
  O.productOddEven();

  return 0;
}

Output

Enter Range : 1 6
Product of all even numbers is 48
Product of all odd numbers is 15

Explanation

In the above code, we have created a class OddEven, two int type data members start and end to store the start and end of the range, and public member functions getRange() and productOddEven() to store range and to print product of the all odd and even numbers in between that given range.

In the main() function, we are creating an object O of class OddEven, reading a range by the user using getRange() function, and finally calling the productOddEven() member function to print the product of all odd and even numbers in the given range. The productOddEven() function contains the logic to print the product of all odd and even numbers in the given range and printing the result.

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

Comments and Discussions!

Load comments ↻





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