c sharp

How to Use BigInteger in C#

As a C# developer, you may often encounter situations where you need to perform arithmetic operations on very large integers that cannot be handled by the standard data types. This is where BigInteger in C# comes in handy. With BigInteger, you can perform mathematical calculations on arbitrarily large integers, making it a valuable tool for developers working on projects that require precise and complex calculations.

In this guide, we will take a more in-depth look at BigInteger in C#.

What is a BigInteger in C#

BigInteger is a class in C# that allows you to work with extremely large integers that are beyond the capacity of conventional data types like int and long. The data type BigInteger is used when we need to perform arithmetic operations on large integers. Working with BigInteger is quite easy in C# and it provides many methods to make arithmetic operations with ease.

The BigInteger class in C# resides in the System.Numerics namespace. BigInteger overcomes the limitations of int and long data types, which can represent numbers up to 2,147,483,647 and 9,223,372,036,854,775,807, respectively. BigInteger allows us to work with very large integers that can be hundreds or even thousands of digits long.

How to Use BigInteger in C#

You must first install and import the System.Numerics namespace into your code before you can use BigInteger. Add the following line to the top of your file to import it after the installation:

using System.Numerics;

 
You can initialize a BigInteger variable using various methods, such as with the constructor, Parse method, or in-place initialization.

BigInteger num1 = BigInteger.Parse("123456789012345678901234567890");

 
BigInteger provides a lot of arithmetic operations that can be done to work with large numbers. We can add, subtract, multiply, and divide using standard arithmetic notations. These operations are implemented through the overloaded operators such as +, -, *, /, and %. The BigInteger class also offers a range of utilities like Abs, Pow, ModInverse, and many more which make computations an easy task.

Like any class or data type, BigInteger offers some properties and methods that can be used to check the characteristics of the object or perform a non-arithmetic operation like Parse, or TryParse. We can check if the number is odd or even with the IsEven and IsOdd properties of the BigInteger class.

Here is a C# code demonstrating how to use the BigInteger class.

using System;
using System.Numerics;

class Program
{
    static void Main(string[] args)
    {
        BigInteger num1 = BigInteger.Parse("123456789012345678901234567890");
        BigInteger num2 = BigInteger.Parse("987654321098765432109876543210");

        BigInteger sum = num1 + num2;
        BigInteger difference = num1 - num2;

        bool equal = num1 == num2;
        bool greaterThan = num1 > num2;
        bool lessThan = num1 < num2;

        bool isEven = num1.IsEven;

        Console.WriteLine("num1 = " + num1);
        Console.WriteLine("num2 = " + num2);
        Console.WriteLine("num1 + num2 = " + sum);
        Console.WriteLine("num1 - num2 = " + difference);
        Console.WriteLine("num1 == num2? " + equal);
        Console.WriteLine("num1 > num2? " + greaterThan);
        Console.WriteLine("num1 < num2? " + lessThan);
        Console.WriteLine("Is the number even? " + isEven);

        Console.ReadLine();
    }
}

 
The above code first generates two BigInteger objects, num1, and num2, by parsing the string representations of very large numbers. It then performs fundamental arithmetic operations, such as addition and subtraction, on these objects and stores the results in new BigInteger objects, sum, and difference.

The code then compares the values of num1 and num2 using the ==, >, and operators, and stores the results in Boolean variables equal, greaterThan, and lessThan, in that order.

Finally, the code checks whether num1 is even using BigInteger’s IsEven property and puts the result in a Boolean variable, isEven. All these variables’ values are then reported to the console using Console.WriteLine().

Output


BigInteger is slower than int and long data types as it involves more calculations to perform the same operations. So, it is a good practice to use BigInteger only when it is necessary to work with large integers. Also, try to use the integer data type which is best suited for the given scenario.

Conclusion

The BigInteger class in C# provides developers with a very flexible tool to work with huge numbers. It provides a lot of operations that can be used in various scenarios, and the syntax is quite like other integer data types in C#. When working with huge integers, BigInteger is the way to go, in C#. With many arithmetic operations available and many utilities, it makes working with very large integers a lot easier.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.