diff --git a/snippets/csharp/language-reference/operators/DivisionExamples.cs b/snippets/csharp/language-reference/operators/DivisionExamples.cs new file mode 100644 index 00000000000..4633169707e --- /dev/null +++ b/snippets/csharp/language-reference/operators/DivisionExamples.cs @@ -0,0 +1,64 @@ +using System; + +namespace operators +{ + public static class DivisionExamples + { + public static void Examples() + { + IntegerDivision(); + IntegerAsFloatingPointDivision(); + FloatingPointDivision(); + DivisionAssignment(); + } + + private static void IntegerDivision() + { + // + Console.WriteLine(13 / 5); // output: 2 + Console.WriteLine(-13 / 5); // output: -2 + Console.WriteLine(13 / -5); // output: -2 + Console.WriteLine(-13 / -5); // output: 2 + // + } + + private static void IntegerAsFloatingPointDivision() + { + // + Console.WriteLine(13 / 5.0); // output: 2.6 + + int a = 13; + int b = 5; + Console.WriteLine((double)a / b); // output: 2.6 + // + } + + private static void FloatingPointDivision() + { + // + Console.WriteLine(16.8f / 4.1f); + Console.WriteLine(16.8d / 4.1d); + Console.WriteLine(16.8m / 4.1m); + // Output: + // 4.097561 + // 4.09756097560976 + // 4.0975609756097560975609756098 + // + } + + private static void DivisionAssignment() + { + // + int a = 4; + int b = 5; + a /= b; + Console.WriteLine(a); // output: 0 + + double x = 4; + double y = 5; + x /= y; + Console.WriteLine(x); // output: 0.8 + // + } + } +} \ No newline at end of file diff --git a/snippets/csharp/language-reference/operators/Program.cs b/snippets/csharp/language-reference/operators/Program.cs index 1617f6fc6b5..cd0be7531b8 100644 --- a/snippets/csharp/language-reference/operators/Program.cs +++ b/snippets/csharp/language-reference/operators/Program.cs @@ -37,6 +37,10 @@ static void Main(string[] args) Console.WriteLine("============== -- and ++ operator examples ====="); DecrementAndIncrementExamples.Examples(); Console.WriteLine(); + + Console.WriteLine("============== / operator examples ============="); + DivisionExamples.Examples(); + Console.WriteLine(); } } }