WAP to calculate Factorial of a number (i) using recursion, (ii) using iteration





#include<iostream.h>
#include<conio.h>
class factorial
{
int f;
public:
int faci(int f);
int facr(int f);
};
int factorial::faci(int f)
{   int pro=1;
                while(f>0)
                {
                pro=pro*f;
                f=f--;
                }
return(pro);
}

int factorial::facr(int f1)
{
                if(f1<=1)
                return (1);
                else
                return(f1*(facr(f1-1)));
}
void main()
{
clrscr();
factorial obj;
int n1,n2;
cout<<"enter the number whose factorial is to be found by iterative method"<<endl;
cin>>n1;
   cout<<"the answer is "<<obj.faci(n1)<<endl;
cout<<"enter the number whose factorial is to be found by recursive method"<<endl;
cin>>n2;
cout<<"the answer is "<<obj.facr(n2)<<endl;
getch();
}


Output:- 

Comments