Write a C++ Program using Recursive Function to find the Factorial of a number.
#include<iostream.h>
#include<conio.h>
long fact(int n);
void main()
{
int n;
long int f;
clrscr();
cout<<endl<<endl;
cout<<" PROGRAM TO FIND FACTORIAL OF NUMBER\n";
cout<<"============================================\n\n";
cout<<"Enter a number to find its factorial: ";
cin>>n;
f=fact(n);
cout<<"The factorial of "<<n<<" is "<<f<<endl;
cout<<"============================================\n";
getch();
}
long fact(int p)
{
long ans;
if(p==1)
return 1;
ans=fact(p-1)*p;
return ans;
}
#include<conio.h>
long fact(int n);
void main()
{
int n;
long int f;
clrscr();
cout<<endl<<endl;
cout<<" PROGRAM TO FIND FACTORIAL OF NUMBER\n";
cout<<"============================================\n\n";
cout<<"Enter a number to find its factorial: ";
cin>>n;
f=fact(n);
cout<<"The factorial of "<<n<<" is "<<f<<endl;
cout<<"============================================\n";
getch();
}
long fact(int p)
{
long ans;
if(p==1)
return 1;
ans=fact(p-1)*p;
return ans;
}
No comments
Post your comments