C++ Program to find factorial of a number using class and object


In this program, You will learn how to find factorial of a number using class and object in C++.


3! = 1 * 2 * 3

4! = 1 * 2 * 3 * 4

5! = 1 * 2 * 3 * 4 * 5


Example: How to find factorial of a number using class and object in C++

Run it Now
#include<iostream>
using namespace std;

class Test {

public:
    int factorial(int x) {
        int i, f = 1;
        for (i = 1; i <= x; i++) {
            f = f*i;
        }
        return f;
    }
};

int main() {

    int x, f;

    cout << "Enter a Number :";
    cin >> x;

    Test obj;
    f = obj.factorial(x);

    cout << "\nThe Factorial is :" << f;

    return 0;
}

Output:

Enter a Number :4
The Factorial is :24