what will be the output of following code:

```
void fun(int x)
{
if(x>0)
{
fun(--x);
printf("%d ",x);
fun(--x);
}
}
int main()
{
int a=4;
fun(a);
getch();
return 0;
```

}

please explain it too!

what will be the output of following code:

```
void fun(int x)
{
if(x>0)
{
fun(--x);
printf("%d ",x);
fun(--x);
}
}
int main()
{
int a=4;
fun(a);
getch();
return 0;
```

}

please explain it too!

Before explaining you the output for `fun(4)`

, let us see outputs for numbers less than 4 -:

Let us denote the ouput for `fun(n)`

as " out(n) "

**1). fun(0)**

In this case ,there will be no output as the `if(n>0)`

condition is not satisfied…

Therefore, out(0) = ( Nothing/Blank output )

**2). fun(1)**

In this case, the condition is satisfied, so `fun(–x)`

will be executed.

Firstly the value of x will decrease by 1, making x=0.

Now `fun(0)`

will be called…Now for a moment, let us forget about this `fun(0)`

's output and denote it as " out(0) " and continue executing the function…

The next line is `printf("%d ",x);`

=> “0” will be the output of this printf statement…

And now here comes our second `fun(–x)`

statement.

Now, first x will become = -1 ( It had become 0 earlier… ) and `fun(-1)`

will be called. ( whose output , just like `fun(0)`

, will again be nothing)

So the final output would be-:

= out(1)

= out(0) + 0 + out(-1)

= nothing + 0 + nothing

=> **0**

**3). fun(2)**

In the same way as mentioned earlier the first output would be “out(1)” ( x became 1 from 2 before it got passed as an argument for `fun(–x)`

), …

Then the printf statement would output “1” …

And then the third output would be “out(0)” ( Again, x became 0 from 1 before being passed as an argument)

Therefore the final output -:

= out(2)

= out(1) + 1 +out(0)

= 0 + 1 + nothing

=> **0 1**

**4). fun(3)**

Similar to the previous case, its output would be -:

= out(3)

= out(2) + 2 + out(1)

= 0 1 + 2 + 0

=> **0 1 2 0**

**5). fun(4)**

Following the same analogy ,

= out(4)

= out(3) + 3 + out(2)

= 0 1 2 0 + 3 + 0 1

=> **0 1 2 0 3 0 1**