News

Tuesday, April 19, 2022

Factorial in C++ Recursive Method





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);

}



Output:




No comments:

Post a Comment