In: Computer Science
Explain the difference between the modulo operator and the division operator for C#
ANSWER :--
Code to test :
using System;
class MainClass {
public static void Main (string[] args) {
Console.WriteLine ("---Modulo Operator (%)---");
Console.WriteLine("Integer remainder:-");
Console.WriteLine(7 % 3); // output: 1
Console.WriteLine(7 % -3); // output: 1
Console.WriteLine(-7 % 3); // output: -1
Console.WriteLine(-7 % -3); // output: -1
Console.WriteLine("Floating-point remainder:--");
Console.WriteLine(-5.2f % 2.0f); // output: -1.2
Console.WriteLine(5.9 % 3.1); // output: 2.8
Console.WriteLine("---Division Operator(/)---");
Console.WriteLine("Integer division:-");
Console.WriteLine(7 / 3); // output: 2
Console.WriteLine(7 / -3); // output: -2
Console.WriteLine(-7 / 3); // output: -2
Console.WriteLine(-7 / -3); // output: 2
Console.WriteLine("Floating-point division:--");
Console.WriteLine(-5.2f / 2.0f); // output: -2.6
Console.WriteLine(5.9 / 3.1); // output: 1.90322
}
}
Screenshot of code with output :--