Introduction:
Working Principle:
Example:
Code:
The source code of factorial using recursive method is below:
// Factorial of any number = 1*2*3*...*n
#include <iostream>
using namespace std;
int factorial(int num)
{
if (num== 0)
{
return 1 ;
}
else
{
return num * factorial(num - 1);
}
}
int main()
{
int num;
cout << "Enter a non-negative number: ";
cin >> num;
cout << "Factorial of " << num << " = " << factorial(num);
}
No comments:
Post a Comment