Skip to content

Commit f46abac

Browse files
authored
Add DivRem() method to C# Math Functions (#7965)
* Add DivRem() method to C# Math Functions * minor content fixes * note fix n grammar fix ---------
1 parent 42b11dc commit f46abac

File tree

1 file changed

+97
-0
lines changed
  • content/c-sharp/concepts/math-functions/terms/divrem

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
Title: '.DivRem()'
3+
Description: 'Calculates the quotient of two numbers and returns the remainder in an output parameter.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Arithmetic'
9+
- 'Methods'
10+
- 'Numbers'
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+
public static long DivRem(long dividend, long divisor, out long remainder);
22+
```
23+
24+
**Parameters:**
25+
26+
- `dividend`: The number to be divided (type `int` or `long`).
27+
- `divisor`: The number to divide by (type `int` or `long`).
28+
- `remainder`: An output parameter that receives the remainder (type `int` or `long`).
29+
30+
**Return value:**
31+
32+
The method returns the quotient as an `int` or `long`, depending on the parameter types used.
33+
34+
> **Note:** This method throws a `DivideByZeroException` if the `divisor` is zero.
35+
36+
## Example
37+
38+
The following example demonstrates using the `.DivRem()` method to calculate both quotient and remainder:
39+
40+
```cs
41+
using System;
42+
43+
public class Example
44+
{
45+
public static void Main()
46+
{
47+
int dividend = 30;
48+
int divisor = 7;
49+
int remainder;
50+
51+
int quotient = Math.DivRem(dividend, divisor, out remainder);
52+
53+
Console.WriteLine($"Dividend: {dividend}");
54+
Console.WriteLine($"Divisor: {divisor}");
55+
Console.WriteLine($"Quotient: {quotient}");
56+
Console.WriteLine($"Remainder: {remainder}");
57+
}
58+
}
59+
```
60+
61+
The example above produces the following output:
62+
63+
```shell
64+
Dividend: 30
65+
Divisor: 7
66+
Quotient: 4
67+
Remainder: 2
68+
```
69+
70+
## Codebyte Example
71+
72+
The following runnable example shows how `.DivRem()` can be used with different numbers:
73+
74+
```codebyte/csharp
75+
using System;
76+
77+
public class Program
78+
{
79+
public static void Main()
80+
{
81+
// Example with positive numbers
82+
int remainder1;
83+
int quotient1 = Math.DivRem(100, 13, out remainder1);
84+
Console.WriteLine($"100 divided by 13 = {quotient1} remainder {remainder1}");
85+
86+
// Example with larger numbers using long
87+
long remainder2;
88+
long quotient2 = Math.DivRem(1000000L, 777L, out remainder2);
89+
Console.WriteLine($"1000000 divided by 777 = {quotient2} remainder {remainder2}");
90+
91+
// Example with negative dividend
92+
int remainder3;
93+
int quotient3 = Math.DivRem(-25, 4, out remainder3);
94+
Console.WriteLine($"-25 divided by 4 = {quotient3} remainder {remainder3}");
95+
}
96+
}
97+
```

0 commit comments

Comments
 (0)