-1

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?

jwpfox
  • 5,124
  • 11
  • 45
  • 42
deviousPriest
  • 13
  • 1
  • 5

2 Answers2

0

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

15 / 6 = 2*6 + 3

So 15 % 6 is 3 :)

MacakM
  • 1,804
  • 3
  • 23
  • 46
0

because x % = y; is expended to x = x % y; and % gives you remainder. So when you do 15 % 6 it will give you 3.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197