Mega Code Archive

 
Categories / C# Book / 02 Essential Types
 

0326 BigInteger calculation

BigInteger overloads all the arithmetic operators, as well as the comparison, equality, modulus (%), and negation operators. using System; using System.Numerics; class Sample { public static void Main() { BigInteger b = BigInteger.Pow(10, 20); Console.WriteLine(b * b); } } The output: 10000000000000000000000000000000000000000 Add two big numbers together. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Numerics; class MainClass { static void Main(string[] args) { // Create a new big integer. BigInteger myBigInt = BigInteger.Multiply(Int64.MaxValue, 2); // Add another value. myBigInt = BigInteger.Add(myBigInt, Int64.MaxValue); // Print out the value. Console.WriteLine("Big Integer Value: {0}", myBigInt); // Wait to continue. Console.WriteLine("\n\nMain method complete. Press Enter"); Console.ReadLine(); } } The output: Big Integer Value: 27670116110564327421