📅  最后修改于: 2023-12-03 14:59:40.509000             🧑  作者: Mango
Modulo operator or remainder operator is used to get the remainder of two numbers. In C#, the modulo operator is represented by the % symbol. The modulo operator can be used with both integers and floating-point numbers.
x % y
int a = 10;
int b = 3;
int c = a % b;
Console.WriteLine(c); // Output: 1
In the above example, we have two integers, a
and b
, and we are finding the modulo of a
and b
. The result is stored in variable c
, which is then printed on the console.
double a = 10.6;
double b = 2.5;
double c = a % b;
Console.WriteLine(c); // Output: 0.6
In the above example, we have two floating-point numbers, a
and b
, and we are finding the modulo of a
and b
. The result is stored in variable c
, which is then printed on the console.
int a = -10;
int b = 3;
int c = a % b;
Console.WriteLine(c); // Output: -1
In the above example, we have an integer a
which is negative and a positive integer b
. The modulo of a
and b
is calculated, and the result is stored in variable c
. As the dividend a
is negative, the result is also negative.
In conclusion, the modulo operator in C# can be used to find the remainder of two numbers. The operator can be used with both integers and floating-point numbers. It is represented by the % symbol.