Click here to Skip to main content
13,044,543 members (59,825 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 25 Oct 2010

An introduction to Big Integer class - Dotnet 4.0

, 26 Oct 2010
Rate this:
Please Sign up or sign in to vote.
This short article will show some of the benifits that the BigInteger class provides


BigInteger class that resides in the System.Numerics namespace helps in representing any large integer without any loss of precision. It has been introduced earlier in dotnet framework 3.5 but was removed(don’t know the exact reason). However, it is again back in framework 4.0

Using the code

Example 1: To find the factorial of 100.

static void Main(string[] args)
    Console.Write("Factorial of hundred is :  ");

/// <summary />
/// Function: HundredFactorial
/// Factorial of Hundred
/// </summary />
private static void HundredFactorial()
    BigInteger number = 100;
    BigInteger fact = 1;
    for (; number-- > 0; ) fact *= number+1;



The program is pretty simple only. We have used the new Go To operator for looping through the elements in order to get the factorial.

Example 2:Find the Sum Of First One Lac Even FibonacciSeries

static void Main(string[] args)
Console.WriteLine("The Sum Of First One Lac Even FibonacciSeries is : ");

/// <summary />
/// Function: SumOfFirstOneLacEvenFibonacciSeries
/// To find the Sum Of First One Lac Even FibonacciSeries
/// </summary />
private static void SumOfFirstOneLacEvenFibonacciSeries()
    int limit = 100000;

    BigInteger num1 = 1;
    BigInteger num2 = 2;
    BigInteger sum = 0;
    BigInteger evenSum = num1 + num2;
    for (int i = 2; i < limit; i++)
        sum =  num1 + num2;
        if (sum % 2 == 0) evenSum += sum;
        num1 = num2;
        num2 = sum;



A Fibonacci series goes like 1,2,3,5,8,11...etc.

i.e. 1+2 =3, 2+3 =5, 3+5 = 8 etc.

The program finds the Fibonacci number till one lac and gives the sum of only the even Fibonacci numbers.

The output being


Some BigIntMethods


Program to find the GCM of two numbers

Console.WriteLine("</span />GCM = "</span /> +  BigInteger.GreatestCommonDivisor(12</span />, 24</span />));

Output is: 12


Compares two System.Numerics.BigInteger values and returns an integer that indicates whether the first value is less than, equal to, or greater than the second value.

BigInteger num1 = 10;
BigInteger num2 = 100;

switch (BigInteger.Compare(num1, num2))
  case -1: Console.WriteLine("{0} is less than {1}", num1, num2);
  case 0: Console.WriteLine("{0} is equal to  {1}", num1, num2);
  case 1: Console.WriteLine("{0} is greater then  {1}", num1, num2);


Converts the string representation of a number to its System.Numerics.BigInteger equivalent.e.g.

BigInteger.Parse("</span />10000000"</span />)


Negates a specified System.Numerics.BigInteger value.e.g.

Console.WriteLine(BigInteger.Negate(-100</span />));

results in 100 and

Console.WriteLine(BigInteger.Negate(100</span />));

results in -100

e)Sign Read property.

Gets a number that indicates the sign (negative, positive, or zero) of the current System.Numerics.BigInteger object. e.g.

Console.WriteLine(BigInteger.Negate(0</span />).Sign);

will give 0

Console.WriteLine(BigInteger.Negate(-1</span />).Sign);

will give 1


Console.WriteLine(BigInteger.Negate(1</span />).Sign);

will give -1

Like these, there are many properties and methods of this class that helps to do very big mathematical calculations.

Type cast

Conversion of BigInteger to standard numeric type and viceversa are both possible.

//Conversion(int to BigInterger)
 int i = 100;
 BigInteger bI = (BigInteger)i;

 //Conversion (BigInteger to int)
 BigInteger BI = 200;
 int j = (int)BI;


In this article we have seen some of the usage of BigInteger class, its usefulness in larger number calculations and some of the built-in methods and properties of this class.

Comments on the topic are highly appreciated for the improvement of the topic.

Thanks for reading the article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Batzen28-Oct-10 1:51
memberBatzen28-Oct-10 1:51 
GeneralMy vote of 1 Pin
SledgeHammer0127-Oct-10 7:25
memberSledgeHammer0127-Oct-10 7:25 
GeneralMy vote of 1 Pin
Argyle4Ever27-Oct-10 4:25
memberArgyle4Ever27-Oct-10 4:25 
GeneralMy vote of 2 Pin
Daniel Brännström26-Oct-10 9:44
memberDaniel Brännström26-Oct-10 9:44 
GeneralMy vote of 2 Pin
PeteSon_Q26-Oct-10 5:34
memberPeteSon_Q26-Oct-10 5:34 
GeneralMy vote of 1 Pin
Selvin26-Oct-10 4:31
memberSelvin26-Oct-10 4:31 
GeneralIt is BigInteger, not BIG INTEGER Pin
leppie26-Oct-10 1:58
memberleppie26-Oct-10 1:58 
GeneralRe: It is BigInteger, not BIG INTEGER Pin
Niladri_Biswas26-Oct-10 3:52
memberNiladri_Biswas26-Oct-10 3:52 
GeneralMy vote of 1 Pin
leppie26-Oct-10 1:57
memberleppie26-Oct-10 1:57 
GeneralRe: My vote of 1 Pin
Niladri_Biswas26-Oct-10 23:54
memberNiladri_Biswas26-Oct-10 23:54 
GeneralRe: My vote of 1 Pin
leppie27-Oct-10 0:29
memberleppie27-Oct-10 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 27 Oct 2010
Article Copyright 2010 by Niladri_Biswas
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid