Export (0) Print
Expand All

Math.DivRem Method (Int32, Int32, Int32)

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function DivRem ( _
	a As Integer, _
	b As Integer, _
	<OutAttribute> ByRef result As Integer _
) As Integer

Parameters

a
Type: System.Int32

The dividend.

b
Type: System.Int32

The divisor.

result
Type: System.Int32

The remainder.

Return Value

Type: System.Int32
The quotient of the specified numbers.

ExceptionCondition
DivideByZeroException

b is zero.

The remainder is calculated by using the modulus operation.

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

Module Example
   Public Sub Main()
      ' Define several positive and negative dividends. 
      Dim dividends() As Integer = { Int32.MaxValue, 13952, 0, -14032, _
                                     Int32.MinValue }
      ' Define one positive and one negative divisor. 
      Dim divisors() As Integer = { 2000, -2000 }

      For Each divisor As Integer In divisors
         For Each dividend As Integer In dividends
            Dim remainder As Integer  
            Dim quotient As Integer = Math.DivRem(dividend, divisor, remainder)
            Console.WriteLine("{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}", _
                              dividend, divisor, quotient, remainder)
         Next 
      Next                                 
   End Sub 
End Module 
' 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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft