0

Explain the code

Output is 012

25th Mar 2020, 6:49 PM
Ajith
Ajith - avatar
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.
25th Mar 2020, 7:00 PM
HonFu
HonFu - avatar
0
void fun(3){ if(n>0){ fun(--n); printf("%d",n); } }
25th Mar 2020, 6:51 PM
Ajith
Ajith - avatar
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.
25th Mar 2020, 6:58 PM
Avinesh
Avinesh - avatar
0
Okie
25th Mar 2020, 6:59 PM
Ajith
Ajith - avatar