-3
int i = 1, j = 2, k;
k = i % ++j

I don't understand why k would equal 1. I figured it what equal 0 because wouldn't the fraction 1/3 round down to 0? Thank you!

  • 2
    That isn't what the modulo operator does. It returns the remainder. Read about it here: http://en.wikipedia.org/wiki/Modulo_operation – wolfPack88 Jun 13 '14 at 15:52

1 Answers1

4

It's actually the remainder. Since three divided into 1 zero times, you have a remainder of one. 1 / 3(using integer arithmetic) is where you would expect the zero.

https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic should be a good link

JoeManiaci
  • 435
  • 3
  • 15
  • 1
    @AssumedVoice The site's Q&A area is not the proper place to have a chat. You can perhaps try http://chat.stackoverflow.com and see if you like it there. – Pascal Cuoq Jun 13 '14 at 16:07
  • @AssumedVoice If Joe's answer above helped, you should consider upvoting AND accepting it (up arrow and checkmark). – Cloud Jun 13 '14 at 16:15