If We want to calculate 100 factorial numbers! we can use data type BigInteger (which is new as of .NET Framework 4.0 and is missing in the older .NET versions). This type represents an integer, which can be very large (for example 100,000 digits). There is no limit on the size of the numbers recorded in the class BigInteger (as long as you have enough RAM).
In order to use BigInteger, we must first add a reference from our project to the assembly System.Numerics.dll (this is a standard .NET library for working with very large integers, which is not referenced by default by our VS projects). Adding a reference to it is done by right-clicking on the current project references in the Solution Explorer window of Visual Studio:
We search and choose the assembly System.Numerics.dll from the list:
Then we need to add "using System.Numerics;" before the beginning of the class of our program and replace decimal withBigInteger. The program obtains the following form:
By BigInteger we can calculate 1000!, 10000! and even 100000! It will take some time, but OverflowException will not occur. The BigInteger class is very powerful but it works many times slower than int and long. For our unpleasant surprise, there is no class "big decimal" in .NET Framework, only "big integer".
In order to use BigInteger, we must first add a reference from our project to the assembly System.Numerics.dll (this is a standard .NET library for working with very large integers, which is not referenced by default by our VS projects). Adding a reference to it is done by right-clicking on the current project references in the Solution Explorer window of Visual Studio:
We search and choose the assembly System.Numerics.dll from the list:
Then we need to add "using System.Numerics;" before the beginning of the class of our program and replace decimal withBigInteger. The program obtains the following form:
using System;
using System.Numerics;
namespace LogicalProgram
{
class Factorial
{
static void Main()
{
Console.WriteLine("Enter the number");
int n = int.Parse(Console.ReadLine());
BigInteger factorial = 1;
while (true)
{
if (n <= 1)
{
break;
}
factorial *= n;
n--;
}
Console.WriteLine("n! = " + factorial);
Console.ReadLine();
}
}
}
Output
1 comment:
Thanks, this is generally helpful.
Still, I followed step-by-step your method in this
dot net training
.net online training hyderabad
Post a Comment