Answers for "factorial recursion c++"

C++
0

factorial using recursion in c++

#include <iostream>
using namespace std;

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

int main()
{
    int number, ans;
    cin >> number;
    ans = factorial(number);
    cout << ans;
    return 0;
}
Posted by: Guest on July-23-2021
8

c++ factorial

#include <cmath>

int fact(int n){
    return std::tgamma(n + 1);  
}    
// for n = 5 -> 5 * 4 * 3 * 2 = 120 
//tgamma performas factorial with n - 1 -> hence we use n + 1
Posted by: Guest on July-09-2020

Code answers related to "factorial recursion c++"

Browse Popular Code Answers by Language