|
| 1 | +--- |
| 2 | +Title: '.DivRem()' |
| 3 | +Description: 'Calculates the quotient of two numbers and returns the remainder in an output parameter.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Code Foundations' |
| 7 | +Tags: |
| 8 | + - 'Methods' |
| 9 | + - 'Numbers' |
| 10 | + - 'Arithmetic' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-c-sharp' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`.DivRem()`** method calculates the quotient of two numbers and returns the remainder in an output parameter. This method is useful when both the quotient and remainder of a division operation are needed. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +Math.DivRem(dividend, divisor, out remainder) |
| 22 | +``` |
| 23 | + |
| 24 | +- `dividend`: The number to be divided (type `int` or `long`). |
| 25 | +- `divisor`: The number to divide by (type `int` or `long`). |
| 26 | +- `remainder`: An output parameter that receives the remainder (type `int` or `long`). |
| 27 | + |
| 28 | +The method returns the quotient as an `int` or `long` depending on the parameter types used. |
| 29 | + |
| 30 | +**Note:** This method throws a `DivideByZeroException` if the `divisor` is zero. |
| 31 | + |
| 32 | +## Example |
| 33 | + |
| 34 | +The following example demonstrates using the `.DivRem()` method to calculate both quotient and remainder: |
| 35 | + |
| 36 | +```cs |
| 37 | +using System; |
| 38 | + |
| 39 | +public class Example |
| 40 | +{ |
| 41 | + public static void Main() |
| 42 | + { |
| 43 | + int dividend = 30; |
| 44 | + int divisor = 7; |
| 45 | + int remainder; |
| 46 | + |
| 47 | + int quotient = Math.DivRem(dividend, divisor, out remainder); |
| 48 | + |
| 49 | + Console.WriteLine($"Dividend: {dividend}"); |
| 50 | + Console.WriteLine($"Divisor: {divisor}"); |
| 51 | + Console.WriteLine($"Quotient: {quotient}"); |
| 52 | + Console.WriteLine($"Remainder: {remainder}"); |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +The example above produces the following output: |
| 58 | + |
| 59 | +```shell |
| 60 | +Dividend: 30 |
| 61 | +Divisor: 7 |
| 62 | +Quotient: 4 |
| 63 | +Remainder: 2 |
| 64 | +``` |
| 65 | + |
| 66 | +## Codebyte Example |
| 67 | + |
| 68 | +The following runnable example shows how `.DivRem()` can be used with different numbers: |
| 69 | + |
| 70 | +```codebyte/csharp |
| 71 | +using System; |
| 72 | +
|
| 73 | +public class Program |
| 74 | +{ |
| 75 | + public static void Main() |
| 76 | + { |
| 77 | + // Example with positive numbers |
| 78 | + int remainder1; |
| 79 | + int quotient1 = Math.DivRem(100, 13, out remainder1); |
| 80 | + Console.WriteLine($"100 divided by 13 = {quotient1} remainder {remainder1}"); |
| 81 | + |
| 82 | + // Example with larger numbers using long |
| 83 | + long remainder2; |
| 84 | + long quotient2 = Math.DivRem(1000000L, 777L, out remainder2); |
| 85 | + Console.WriteLine($"1000000 divided by 777 = {quotient2} remainder {remainder2}"); |
| 86 | + |
| 87 | + // Example with negative dividend |
| 88 | + int remainder3; |
| 89 | + int quotient3 = Math.DivRem(-25, 4, out remainder3); |
| 90 | + Console.WriteLine($"-25 divided by 4 = {quotient3} remainder {remainder3}"); |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
0 commit comments