×

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 all digits of a number using class

Submitted by Shubh Pachori, on August 22, 2022

Problem statement

Given a number, we have to find the product of all digits of a number using the class and object approach.

Example:

Input:
Enter Number: 12345

Output:
Product of all digits of the number is 120

C++ code to find out the product of all digits of a number using the class and object approach

#include <iostream> using namespace std; // create a class class Product { // private data member private: int number; // public functions public: // getNumber() function to get the number void getNumber() { cout << "Enter Number:"; cin >> number; } // multiply() function to find the product of all digits of the number int multiply() { int product = 1, remain; while (number) { // the last digit of number is stored in remain by % operator remain = number % 10; // remain is multiplied in the product product = product * remain; // number is divided by 10 to discard the last digit number = number / 10; } // returning the product return product; } }; int main() { // create a object Product P; // int type variable to store the product of all digits in it int product; // function is called by the object to store the number P.getNumber(); // multiply() function to multiply all digits of the number product = P.multiply(); cout << "Product of all digits of the number is " << product; return 0; }

Output

Enter Number:9876
Product of all digits of the number is 3024

Explanation

In the above code, we have created a class named Product, an int type data member number to store the number, and public member functions getNumber() and multiply() to store and find out the product of all digits of the given number.

In the main() function, we are creating an object P of class Product, reading an integer number by the user using getNumber() function, and finally calling the multiply() member function to find out the product of all digits of a given integer number. The multiply() function contains the logic to find out the product of all digits of the given number and printing the result.

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

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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