Wednesday, March 9, 2016

Find the factorial for the given number

Code:

using System;

public class Test
{
    public static void Main()
    {
    int n = 4;
        
        Console.WriteLine("Input: " + n);
        
Console.WriteLine("Output: Factorial for " + n + " is " + new Test().Factorial(n));
    }

    public int Factorial(int n)
{
if(n == 2)
{
return 2;
}
else
{
return n * Factorial(n - 1);
}
}

}

Output:

Input: 4
Output: Factorial for 4 is 24

Run Time: O(N)

No comments:

Post a Comment