Answers for "Factorial tail recursion"

-1

Factorial tail recursion

nt factTR(unsigned int n, unsigned int a)
{
    if (n == 0)
    {
        return a;
    }
    else
    {
        return factTR(n - 1, n * a);
    }

}

int fact(unsigned int n);
{
    return factTR(n, 1);
}

int main(void)
{
    unsigned int a, n;

    printf("Enter a positive number : ");
    scanf("%u", &n);
    a = fact(n);

    printf("The factorial of %u is %u", n, a);

    return 0;
}
Posted by: Guest on July-06-2021

Browse Popular Code Answers by Language