Wednesday, June 13, 2012

Factorial of a number using class in cpp

//This program is compiled using GCC

#include<iostream>
using namespace std;

class factorial
{
    int num;
   
    public:
        void getinfo()                 //1st member function of class factorial
        {
            cout << "\n Enter the number for which the factorial is to be found: ";
            cin >> num;
        }
       
        void facto()          
//2nd member function of class factorial        
        {
            int i, fact=1;
           
            if(num == 0 || num == 1)
            {
                cout << "\n factorial of "<< num << " is 1";
            }
           
            else
            {
                for(i=1; i<=num; i++)
                {
                    fact = fact*i;
                }
                cout << "\n factorial of " << num << " is: " << fact;
            }
        }
};

int main()
{
    factorial f1;                //creating the object of class 'factorial'
    f1.getinfo();
    f1.facto();
   
    cin.ignore();      //these 2 fun are use
    cin.get();          //for holding the output screen
}

No comments:

Post a Comment