0
Explain the code
Output is 012
4 Answers
+ 1
As long as n>0, the function keeps calling itself with n reduced by one.
As soon 0 is reached, the recursion stack dissolves from the bottom. We're back in the last call, where n was 1 and we reduced it by 1.
So we print 0.
Then we go one step further back where n was 2, but was reduced by one, so we print 1.
And so on.
Try to think through that sort of situation step by step, on paper if you have to, until you're perfectly sure you understood the flow.
0
void fun(3){
if(n>0){
fun(--n);
printf("%d",n);
}
}
0
Ajith this is a recursion case where the values are printed at the time of returning. You should probably take a paper and pen and draw the recursion tree to understand it better.
0
Okie