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++.
6 = 1 * 2 * 3
24 = 1 * 2 * 3 * 4
120 = 1 * 2 * 3 * 4 * 5
Example: How to find factorial of a number using class and object in C++.
#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 << "Factorial is:" << f;
return 0;
}
Output:
Enter a number:4
Factorial is:24