Search code examples
javamodulo

Why does this print out 21 and not 11?


The code I wrote was meant to take the sum of the digits of an integer number, e.g. 143 gives out 8, 020341 give out 10.

This is the code I wrote for it

I intended for this to print out 11 but it gives me 21 instead. It works fine if the first digit isn't a 0, I'm not sure on how to make it so that it works with 0 in the first digit.


Solution

  • With the leading 0, your int is an octal.

    System.out.println(012345 == 12345); // false