📌  相关文章
📜  c# modulo - C# (1)

📅  最后修改于: 2023-12-03 14:59:40.509000             🧑  作者: Mango

C# Modulo

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.

Syntax
x % y
  • x: dividend
  • y: divisor
Examples
Example 1: Integer modulo
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.

Example 2: Floating-point modulo
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.

Example 3: Modulo with negative numbers
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.

Conclusion

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.