0

Mysterious output of static method

Pls explain public class First { public static int Cb(int x){ if(x < 100) x = Cb(x + 10); return(x - 1); } public static void main(String[] args) { System.out.print(First.Cb(60)); //95 } }

9th Mar 2019, 12:26 AM
Ben ogegbo
Ben ogegbo  - avatar
2 Respostas
+ 3
No it is correct.when you enter 60, recursion happens 4 times.and by the 5th recursion,the x value is 100 and if statement is not executed and then x-1 happens 5 times due to return statement.so answer is 95 is correct.
9th Mar 2019, 12:37 AM
Home Number
Home Number - avatar
0
i have found why output is 95. i had to test various return(x - n) n eg 2,3,4.
9th Mar 2019, 3:29 AM
Ben ogegbo
Ben ogegbo  - avatar