Why is the output of the following code 9 Systemoutprintlnaa

Why is the output of the following code 9?

System.out.println(a.answer(70));

private int answer(int k) {

if(k<=5){

return k*3;

}

else{

return answer(answer(k/4));

}

}

Solution

in this code 70 is given as input value

as 70>5

it goes to else statement

return answer(answer(17));

again 17>5

it goes to else statement

return answer(answer(4));

return answer(answer(1));

now the answer(k/4) in answer(answer(k/4)) value is 1

because as 1 <5

so 3*1=3

now answer(answer(k/4))----> answer(3);

3<5

then answer(3)=9

Why is the output of the following code 9? System.out.println(a.answer(70)); private int answer(int k) { if(k<=5){ return k*3; } else{ return answer(answer(k

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site