Factorial of a Number in C++ using Recursion

In this C++ program, we will have a look at the factorial of a number in C++ using Recursion. If a function is called over and over again, the function is regarded as the Recursive Function

Factorial of a Number in C++ using Recursion

#include <iostream>
using namespace std;
int main ()
{
int factorial (int);
int fact, value;
cout << "Please enter a number:";
cin >> value;
fact = factorial (value);
cout << "Factorial of a number is:" << fact << endl;
return 0;
}

int factorial (int n)//function
{
if (n <0)
return (-1);
if (n == 0)
return (1);
else
{
return (n * factorial (n-1));
}
}

Output:

Please enter a number: 5

Factorial of a number is: 120

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping cart