Skip to content

Commit 5463bac

Browse files
pkulikovBillWagner
authored andcommitted
Added snippets for the division operator article (#505)
* Added snippets for the division operator article * Addressed feedback
1 parent 9e5e193 commit 5463bac

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
3+
namespace operators
4+
{
5+
public static class DivisionExamples
6+
{
7+
public static void Examples()
8+
{
9+
IntegerDivision();
10+
IntegerAsFloatingPointDivision();
11+
FloatingPointDivision();
12+
DivisionAssignment();
13+
}
14+
15+
private static void IntegerDivision()
16+
{
17+
// <SnippetInteger>
18+
Console.WriteLine(13 / 5); // output: 2
19+
Console.WriteLine(-13 / 5); // output: -2
20+
Console.WriteLine(13 / -5); // output: -2
21+
Console.WriteLine(-13 / -5); // output: 2
22+
// </SnippetInteger>
23+
}
24+
25+
private static void IntegerAsFloatingPointDivision()
26+
{
27+
// <SnippetIntegerAsFloatingPoint>
28+
Console.WriteLine(13 / 5.0); // output: 2.6
29+
30+
int a = 13;
31+
int b = 5;
32+
Console.WriteLine((double)a / b); // output: 2.6
33+
// </SnippetIntegerAsFloatingPoint>
34+
}
35+
36+
private static void FloatingPointDivision()
37+
{
38+
// <SnippetFloatingPoint>
39+
Console.WriteLine(16.8f / 4.1f);
40+
Console.WriteLine(16.8d / 4.1d);
41+
Console.WriteLine(16.8m / 4.1m);
42+
// Output:
43+
// 4.097561
44+
// 4.09756097560976
45+
// 4.0975609756097560975609756098
46+
// </SnippetFloatingPoint>
47+
}
48+
49+
private static void DivisionAssignment()
50+
{
51+
// <SnippetDivisionAssignment>
52+
int a = 4;
53+
int b = 5;
54+
a /= b;
55+
Console.WriteLine(a); // output: 0
56+
57+
double x = 4;
58+
double y = 5;
59+
x /= y;
60+
Console.WriteLine(x); // output: 0.8
61+
// </SnippetDivisionAssignment>
62+
}
63+
}
64+
}

snippets/csharp/language-reference/operators/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ static void Main(string[] args)
3737
Console.WriteLine("============== -- and ++ operator examples =====");
3838
DecrementAndIncrementExamples.Examples();
3939
Console.WriteLine();
40+
41+
Console.WriteLine("============== / operator examples =============");
42+
DivisionExamples.Examples();
43+
Console.WriteLine();
4044
}
4145
}
4246
}

0 commit comments

Comments
 (0)