Modulo Operator in C#
This tutorial article will introduce the modulo operator in C# programming.
Get the Remainder Using the Modulo Operator in C#
The terms used in the modulo operation are as follows:
- Divisor: the value on which we divide any value;
- Dividend: the value to which we divide by another value;
- Quotient: the value that we get from the division operation;
- Remainder: the value that we get as the remaining value.
Suppose we will divide the two values 13 and 2. If the quotient is our major concern, we use the division operator and get an answer of 6.5.
And now, suppose we want to get the remaining value from the division of 13 by 2; we denote it by 13%2. It gives us the remainder 1 after division.
There are no decimal places in the modulo operation. If the dividend is greater than the divisor, we divide it and obtain the remainder; otherwise, the dividend is the remainder value.
Let us consider another example: 3 divided by 4. Here 3 is our dividend, and 4 is our divisor, and because 3 is smaller than 4, our remainder is 3 for this expression (3%4).
Below is the code comparison of both division and modulo operations.
Example (division):
public static void divideop() {
double num_1, num_2, result;
Console.WriteLine("enter n1");
num_1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter n2");
num_2 = Convert.ToInt32(Console.ReadLine());
result = num_1 / num_2;
Console.WriteLine("result is: " + result);
Console.ReadLine();
}
Output:
The above code gets two values from the user as input: num_1
and num_2
. The result
variable stores the value after division.
We used a double data type in the above code because it may return a decimal point value.
See the image below to understand how the result is obtained.
Example (modulo operation):
public static void modulusop() {
double num_1, num_2, result;
Console.WriteLine("enter n1");
num_1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter n2");
num_2 = Convert.ToInt32(Console.ReadLine());
result = num_1 % num_2;
Console.WriteLine("result is: " + result);
Console.ReadLine();
}
Output:
Similarly, the above code gets two values: num_1
and num_2
, and the result
variable stores the remainder value after the modulo operation.
Below is an image to understand how the modulo operation is carried out.
Complete Example Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace modulus_operator_A1 {
public class Program {
static void Main(string[] args) {
int i = 1;
Console.WriteLine("enter your chooice 1 for divide operation 2 for modulus operation");
int choice = Convert.ToInt32(Console.ReadLine());
if (choice == 1) {
divideop();
} else if (choice == 2) {
modulusop();
} else {
}
Console.Read();
}
public static void divideop() {
double num_1, num_2, result;
Console.WriteLine("enter n1");
num_1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter n2");
num_2 = Convert.ToInt32(Console.ReadLine());
result = num_1 / num_2;
Console.WriteLine("result is: " + result);
Console.ReadLine();
}
public static void modulusop() {
double num_1, num_2, result;
Console.WriteLine("enter n1");
num_1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter n2");
num_2 = Convert.ToInt32(Console.ReadLine());
result = num_1 % num_2;
Console.WriteLine("result is: " + result);
Console.ReadLine();
}
}
}