program for factorial using recursion, while and for loop in c#

In this article you can see how to calculate factorial of a number using recursive function , while loop and from for loop below are the three function just pass number that you want to calculate factorial and it will return the output (i.e factorial of the number).

check out more programs Reverse number program in c# and Get max occurred character in string

Calculate factorial using recursive function

public double factorial_Recursion(int number)
{
    if (number == 1)
        return 1;
    else
        return number * factorial_recursion(number - 1);
}

Calculate factorial using while loop

public double factorial_WhileLoop(int number)
{
    double result = 1;
    while (number != 1)
    {
        result = result * number;
        number = number - 1;
    }
    return result;
}

Calculate factorial using for loop

public double factorial_ForLoop(int number)
{
    double result = 1;
    for (int i = number; i > 1; i--)
    {
        result = result * i;
    }
    return result;

}



double result = factorial_recursion(11);
double result1 = factorial_WhilLoop(11);
double result2 = factorial_ForLoop(11);



Recent Article