Files
Examples/C#/Factorial/Factorial/Program.cs
2018-05-13 18:27:05 +01:00

47 lines
1.5 KiB
C#

using System;
using System.Numerics;
using System.Diagnostics;
namespace Factorial {
class Program {
static void Main(string[] args) {
bool exit = false;
while (!exit) {
bool correct = false;
BigInteger find = 0;
while (!correct) {
try {
Console.Write("Enter a number to find the factorial of: ");
find = BigInteger.Parse(Console.ReadLine());
correct = true;
} catch {
Console.WriteLine("That is not a number");
}
}
Console.WriteLine($"The factorial of { find } is { Factorial(find) }");
Console.WriteLine("Press any key to exit and enter to try again");
var response = Console.ReadKey();
switch (response.Key) {
case ConsoleKey.Enter:
break;
default:
exit = true;
break;
}
}
}
static BigInteger Factorial(BigInteger arg) {
Stopwatch t = new Stopwatch(); ;
t.Start();
BigInteger value = 1;
for (int i = 2; i <= arg; i++) {
value *= i;
}
t.Stop();
Console.WriteLine("Found in {0} ms", t.Elapsed.TotalMilliseconds);
return value;
}
}
}