java - How to find what value this code returns without a computer? -
what value mystery(12345678) return?
public string mystery(int n) { string soln = “”; while (n > 0) { soln = n % 10 + soln; n = n / 100; } return soln; }
the answers are: a) “1357” b) “2468” c) “7531” d) “8642” e) causes run-time error.
edit: know answer b) compiling not sure how works.
you can see it's loop of adding one's digit of n first index of string, dividing n 100. eventually, 2468
Comments
Post a Comment