Write a C++ program which calculates the factorial of a given number using recursion.
#include<conio.h>
#include<iostream.h>
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}
int main ()
{
long ln;
clrscr();
cout << "Enter a number: ";
cin >> ln;
cout << ln << "!" << " = " << factorial (ln);
getch();
return 0;
}
OUTPUT
You might also like:-
#include<conio.h>
#include<iostream.h>
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}
int main ()
{
long ln;
clrscr();
cout << "Enter a number: ";
cin >> ln;
cout << ln << "!" << " = " << factorial (ln);
getch();
return 0;
}
OUTPUT
You might also like:-