Factorial using Recursion C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Factorial
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(factorial(4));
Console.ReadLine();
}
static int factorial(int n)
{
if (n > 1)
{
return factorial(n - 1) * n;
}
else
{
return 1;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Factorial
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(factorial(4));
Console.ReadLine();
}
static int factorial(int n)
{
if (n > 1)
{
return factorial(n - 1) * n;
}
else
{
return 1;
}
}
}
}
very nice
ReplyDeleteIt was really helpful...Thanks a lot!
ReplyDeleteIt only works up to 12; 13 gave an incorrect value. I had to change the value type to long to get the correct value.
ReplyDelete