# C# what is "%"

I’m having trouble finding this on the internet? If someone could tell me what it does that would be great. it seems to go int%int in a script I’m using.

It’s the modulus operator, and it’s not exclusive to C#.

It will give you the remainder after division.

That’s “Modulus” operator. It will basically divide the first value by the second value and give you the rest.
Example:

4%3 will give you 1 back.
10%8 will return 2.

4%2 will return 0.

1 Like

x = a%b

x - the remainder of division of a by b (I hope you understood me, because I don’t know how to spel it write :))

Ok thanks guys

say 1%10 would that return 9?

1%10 = 1
5%10 = 5

23%10 = 3
35%10 = 5

5.3 % 4 = 1.3
5.3 % 4.5 = 0.8

-3.4 % 2 = -1.4

if you modulus a number by a larger number you just get that number.

1%10=1
2%10=2
3%10=3
etc…
9%10=9
then…
10%10=0

Modulus is often used to test for regular numeric repetition. It can effectively be used to find multiples of a given value.

If you’re cycling through a series of values, and you test for the modulus of a given number, everytime the returned modulus is zero, you will know you’ve found a multiple of that number.

9%3 = 0

9%4 = 1

Why does 9%4 = 1? Because 4 times 2 equals 8, and 9 minus 8 equals 1. You can think of modulus as taking the closest multiplication of the number on the right without going over the number on the left, and then subtracting that value from the number on the left. When the number on the right divides evenly into the number on the left, the subtraction leaves you with zero.

20%5 = 0
27%3 = 0
56%4 = 0
etc…

C# is one of the best languages which you can easily understand the functionality of its formula. This % sign is called modulus in C# which is used to get a particular value of a variable. It gives you the remainder after division like 6%2.