deviousPriest deviousPriest - 26 days ago 17
C# Question

Why does the console write 3?

Why does this make sense? i want someone to explain it for dummies because i'm new to coding (Sorry in advance).

I'm trying to learn c# and i come across this code as a question.

int x = 15;
int y = 6:
x % = y;
Console.Writeline(x);


Console writes 3.

I was reading a comment someone left on the question and he said:

"15 / 6 = 2

6 * 2 = 12

15 - 12 = 3"

This might be so basic to so many of you and i probably sound like the biggest cuck on earth. But is that really how it's calculated?

Answer

Yes, % means modulo. It is the remainer after the division.

15 / 6 = 2*6 + 3

So 15 % 6 is 3 :)

Comments