CPP program to calculate the Factorial of a number using class and object


 /*
Alamgir Hossain
Lecturer, Dept. of CSE, Prime University

CPP program to calculate the Factorial of a number using class and object
5! = 5 * 4 * 3 * 2 * 1 = 120
*/
#include<iostream>
using namespace std;
class factorial{
private:
        int num, fact;
public:
        factorial(int n){//Constructor
                num = n;
        }
        void factorialCalculate(){//Function for calculating factorial
                fact = 1;
                for(int i = num; i >= 1; i--){
                        fact = fact * i;
                }
        }
        void display()
        {
                cout<<"Factorial of: "<<num<<" is: "<<fact<<endl;
        }
};
int main()
{
        factorial obj(7);
        obj.factorialCalculate();
        obj.display();
        return 0;
}



No comments:

Post a Comment