% operator

Updated on July 12, 2017 in [C] Beginner
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on July 11, 2017

What is the output of this code.

int x = 15;
int y = 6;

X %= y

Console. WriteLine (X);

What is the output of this code. Please explain it properly

  • Liked by
Reply
2 on July 12, 2017

After having searched up this operator, I have come to the conclusion that your code will just spit out an error. The (%) Operator is used in this form, this line is from MSDN – The % operator computes the remainder after dividing its first operand by its second. All numeric types have predefined remainder operators, as you can see, in your line of code, there is no second operand, and so therefore it would not work.

This operator is used to get the remainder of a division between int x and int y.

Devoted
on July 12, 2017

No, this code works fine, kinda… This code will throw an error because there isn’t a semicolon at the end of the third line.

You say that this line won’t work because it doesn’t have a second operand.

x %= y;

But it has a second operand, it’s “y”. You can write that line like this also:
x = x % y;

It is exactly the same.

 

But you’re correct saying that it calculates the remainder after a division of two numbers.

Devoted
on July 12, 2017

So then the output of the code would be:
3
 
because 15 % 6 = 3;
If you didn’t know, the % operator gets the remainder after dividing, so…
15 / 6 = 2 remainder 3

Show more replies
  • Liked by
Reply
Cancel