C++ Program : To Find Factorial of a Number using recusion




#include<iostream.h>
#include<conio.h>
class number
{
   int x;
   long ans;
    public:
   long factorial(int);
};
long number :: factorial(int a)
{
   x=a;
   ans=1;
   while(a>0)
       ans*=a--;
   return ans;
}

int main()
{
   clrscr();
   int num;
   cout<<"Enter the number to find factorial:";
   cin>>num;
   number a;
   cout<<"The factorial is : "<<a.factorial(num);
   getch();
   return 0;
}

Class diagram 

(click image to view large)

 

 

0 comments:

Post a Comment