Create a program that will implement a method Factorial and return a double
number based on any supplied argument.
public class HelloWorld
{
static double Factorial(int n)
{
int fact;
if(n == 0)
{
fact = 1;
}
else
{
fact = 1;
for(int i = 1; i < n; i++)
{
fact += fact * i;
}
}
return fact;
}
static double FactorialDouble(double n)
{
int tmp = (int)n;
if (((double) tmp) == n)
n = Factorial(tmp);
else
n = Math.Exp(Math.Log(Factorial(tmp)) + (n - tmp) * Math.Log(tmp+1));
return n;
}
public static void Main(string[] args)
{
Console.Write("Enter number: ");
double number = double.Parse(Console.ReadLine());
Console.WriteLine ($"Factorial == {FactorialDouble(number)}!");
}
}
Comments
Leave a comment