Edit

Share via


Math.DivRem Method

Definition

Calculates the quotient of two numbers and also returns the remainder in an output parameter.

Overloads

DivRem(Int64, Int64, Int64)

Calculates the quotient of two 64-bit signed integers and also returns the remainder in an output parameter.

DivRem(Int32, Int32, Int32)

Calculates the quotient of two 32-bit signed integers and also returns the remainder in an output parameter.

DivRem(UIntPtr, UIntPtr)

Produces the quotient and the remainder of two unsigned native-size numbers.

DivRem(UInt64, UInt64)

Produces the quotient and the remainder of two unsigned 64-bit numbers.

DivRem(UInt32, UInt32)

Produces the quotient and the remainder of two unsigned 32-bit numbers.

DivRem(UInt16, UInt16)

Produces the quotient and the remainder of two unsigned 16-bit numbers.

DivRem(SByte, SByte)

Produces the quotient and the remainder of two signed 8-bit numbers.

DivRem(Int64, Int64)

Produces the quotient and the remainder of two signed 64-bit numbers.

DivRem(Int32, Int32)

Produces the quotient and the remainder of two signed 32-bit numbers.

DivRem(Int16, Int16)

Produces the quotient and the remainder of two signed 16-bit numbers.

DivRem(Byte, Byte)

Produces the quotient and the remainder of two unsigned 8-bit numbers.

DivRem(IntPtr, IntPtr)

Produces the quotient and the remainder of two signed native-size numbers.

DivRem(Int64, Int64, Int64)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Calculates the quotient of two 64-bit signed integers and also returns the remainder in an output parameter.

C#
public static long DivRem(long a, long b, out long result);

Parameters

a
Int64

The dividend.

b
Int64

The divisor.

result
Int64

When this method returns, contains the remainder.

Returns

The quotient of the specified numbers.

Exceptions

Examples

The following example demonstrates the DivRem(Int64, Int64, Int64) method.

C#
using System;

public class Example
{
   public static void Main()
   {
      // Define several positive and negative dividends.
      long[] dividends = { Int64.MaxValue, 13952, 0, -14032,
                           Int64.MinValue };
      // Define one positive and one negative divisor.
      long[] divisors = { 2000, -2000 };

      foreach (long divisor in divisors)
      {
         foreach (long dividend in dividends)
         {
            long remainder;
            long quotient = Math.DivRem(dividend, divisor, out remainder);
            Console.WriteLine(@"{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}",
                              dividend, divisor, quotient, remainder);
         }
      }
   }
}
// The example displays the following output:
//    9,223,372,036,854,775,807 \ 2,000 = 4,611,686,018,427,387, remainder 1,807
//    13,952 \ 2,000 = 6, remainder 1,952
//    0 \ 2,000 = 0, remainder 0
//    -14,032 \ 2,000 = -7, remainder -32
//    -9,223,372,036,854,775,808 \ 2,000 = -4,611,686,018,427,387, remainder -1,808
//    9,223,372,036,854,775,807 \ -2,000 = -4,611,686,018,427,387, remainder 1,807
//    13,952 \ -2,000 = -6, remainder 1,952
//    0 \ -2,000 = 0, remainder 0
//    -14,032 \ -2,000 = 7, remainder -32
//    -9,223,372,036,854,775,808 \ -2,000 = 4,611,686,018,427,387, remainder -1,808

Remarks

The remainder value equals the result of the remainder operator.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DivRem(Int32, Int32, Int32)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Calculates the quotient of two 32-bit signed integers and also returns the remainder in an output parameter.

C#
public static int DivRem(int a, int b, out int result);

Parameters

a
Int32

The dividend.

b
Int32

The divisor.

result
Int32

When this method returns, contains the remainder.

Returns

The quotient of the specified numbers.

Exceptions

Examples

The following example demonstrates the DivRem(Int32, Int32, Int32) method.

C#
using System;

public class Example
{
   public static void Main()
   {
      // Define several positive and negative dividends.
      int[] dividends = { Int32.MaxValue, 13952, 0, -14032,
                                     Int32.MinValue };
      // Define one positive and one negative divisor.
      int[] divisors = { 2000, -2000 };

      foreach (int divisor in divisors)
      {
         foreach (int dividend in dividends)
         {
            int remainder;
            int quotient = Math.DivRem(dividend, divisor, out remainder);
            Console.WriteLine(@"{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}",
                              dividend, divisor, quotient, remainder);
         }
      }
   }
}
// The example displays the following output:
//       2,147,483,647 \ 2,000 = 1,073,741, remainder 1,647
//       13,952 \ 2,000 = 6, remainder 1,952
//       0 \ 2,000 = 0, remainder 0
//       -14,032 \ 2,000 = -7, remainder -32
//       -2,147,483,648 \ 2,000 = -1,073,741, remainder -1,648
//       2,147,483,647 \ -2,000 = -1,073,741, remainder 1,647
//       13,952 \ -2,000 = -6, remainder 1,952
//       0 \ -2,000 = 0, remainder 0
//       -14,032 \ -2,000 = 7, remainder -32
//       -2,147,483,648 \ -2,000 = 1,073,741, remainder -1,648

Remarks

The remainder value equals the result of the remainder operator.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DivRem(UIntPtr, UIntPtr)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Important

This API is not CLS-compliant.

Produces the quotient and the remainder of two unsigned native-size numbers.

C#
[System.CLSCompliant(false)]
public static(nuint Quotient, nuint Remainder) DivRem(nuint left, nuint right);
C#
[System.CLSCompliant(false)]
public static(UIntPtr Quotient, UIntPtr Remainder) DivRem(UIntPtr left, UIntPtr right);

Parameters

left

nuint

The dividend.

right

nuint

The divisor.

Returns

ValueTuple<nuint,nuint>

The quotient and the remainder of the specified numbers.

Attributes

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

DivRem(UInt64, UInt64)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Important

This API is not CLS-compliant.

Produces the quotient and the remainder of two unsigned 64-bit numbers.

C#
[System.CLSCompliant(false)]
public static(ulong Quotient, ulong Remainder) DivRem(ulong left, ulong right);

Parameters

left
UInt64

The dividend.

right
UInt64

The divisor.

Returns

The quotient and the remainder of the specified numbers.

Attributes

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

DivRem(UInt32, UInt32)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Important

This API is not CLS-compliant.

Produces the quotient and the remainder of two unsigned 32-bit numbers.

C#
[System.CLSCompliant(false)]
public static(uint Quotient, uint Remainder) DivRem(uint left, uint right);

Parameters

left
UInt32

The dividend.

right
UInt32

The divisor.

Returns

The quotient and the remainder of the specified numbers.

Attributes

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

DivRem(UInt16, UInt16)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Important

This API is not CLS-compliant.

Produces the quotient and the remainder of two unsigned 16-bit numbers.

C#
[System.CLSCompliant(false)]
public static(ushort Quotient, ushort Remainder) DivRem(ushort left, ushort right);

Parameters

left
UInt16

The dividend.

right
UInt16

The divisor.

Returns

The quotient and the remainder of the specified numbers.

Attributes

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

DivRem(SByte, SByte)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Important

This API is not CLS-compliant.

Produces the quotient and the remainder of two signed 8-bit numbers.

C#
[System.CLSCompliant(false)]
public static(sbyte Quotient, sbyte Remainder) DivRem(sbyte left, sbyte right);

Parameters

left
SByte

The dividend.

right
SByte

The divisor.

Returns

The quotient and the remainder of the specified numbers.

Attributes

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

DivRem(Int64, Int64)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Produces the quotient and the remainder of two signed 64-bit numbers.

C#
public static(long Quotient, long Remainder) DivRem(long left, long right);

Parameters

left
Int64

The dividend.

right
Int64

The divisor.

Returns

The quotient and the remainder of the specified numbers.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

DivRem(Int32, Int32)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Produces the quotient and the remainder of two signed 32-bit numbers.

C#
public static(int Quotient, int Remainder) DivRem(int left, int right);

Parameters

left
Int32

The dividend.

right
Int32

The divisor.

Returns

The quotient and the remainder of the specified numbers.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

DivRem(Int16, Int16)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Produces the quotient and the remainder of two signed 16-bit numbers.

C#
public static(short Quotient, short Remainder) DivRem(short left, short right);

Parameters

left
Int16

The dividend.

right
Int16

The divisor.

Returns

The quotient and the remainder of the specified numbers.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

DivRem(Byte, Byte)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Produces the quotient and the remainder of two unsigned 8-bit numbers.

C#
public static(byte Quotient, byte Remainder) DivRem(byte left, byte right);

Parameters

left
Byte

The dividend.

right
Byte

The divisor.

Returns

The quotient and the remainder of the specified numbers.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

DivRem(IntPtr, IntPtr)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Produces the quotient and the remainder of two signed native-size numbers.

C#
public static(nint Quotient, nint Remainder) DivRem(nint left, nint right);
C#
public static(IntPtr Quotient, IntPtr Remainder) DivRem(IntPtr left, IntPtr right);

Parameters

left

nint

The dividend.

right

nint

The divisor.

Returns

ValueTuple<nint,nint>

The quotient and the remainder of the specified numbers.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9