Wednesday, October 12, 2011

Challenge 019

int result = 13 - 3 * 6 / 4 % 3;

This string gives you an output of 12.

you follow order of operations so:

3 * 6 = 18
18 / 4 = 4.5 ; since result is declared an integer, 4.5 is truncated to simply 4
4 / 3 = 1 with a remainder of 1 ; because of the % , this results in a value of 1
13 - 1 = 12

12 is the result and remains an integer.

3 comments:

  1. Perfect analysis, Ryan, one thing though:

    "This string gives you an output of 12."

    There is a technical inaccuracy in referring to this as a string. Please revise.

    ReplyDelete
  2. this statement* gives you an output of 12

    ReplyDelete
  3. Most excellent, Ryan. 15 XPs awarded for this completion.

    ReplyDelete