Divide BigInteger, get the result and remainder in CSharp

Description

The following code shows how to divide BigInteger, get the result and remainder.

Example


/*w ww.  j a va 2s.  c om*/
using System;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      BigInteger divisor = BigInteger.Pow(Int64.MaxValue, 2);

      BigInteger[] dividends = { BigInteger.Multiply((BigInteger) Single.MaxValue, 2), 
                                 BigInteger.Parse("9999999999999999999999999999999999999"), 
                                 BigInteger.One, 
                                 BigInteger.Multiply(Int32.MaxValue, Int64.MaxValue),
                                 };

      // Divide each dividend by divisor in three different ways.
      foreach (BigInteger dividend in dividends)
      {
         BigInteger quotient;
         BigInteger remainder = 0;

         Console.WriteLine(BigInteger.Divide(dividend, divisor));
         Console.WriteLine(dividend / divisor);
         quotient = BigInteger.DivRem(dividend, divisor, out remainder);
         Console.WriteLine("   Using DivRem method:     {0:N0}, remainder {1:N0}", quotient, remainder);
      }            
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var