in Programming
211 views
1 vote
1 vote
#include <stdio.h>
int fun(int num){
    while(num>0)
    {
        num=num*fun(num-1);
    }
    return num;
}
int main()
{
    int x=fun(8);
    printf("%d",x);
    return 0;
}

Hello Folks, I have a doubt related to the above snippet of code.

Why does the output of the above code be 0?

Kindly help me with a detailed explanation.
in Programming
211 views

1 Answer

3 votes
3 votes
Let's check at the fun(0)

it doesn't enter into for loop

and returns zero, which is multiplied by every function and returns num=num*0

which means zero at the main function

3 Comments

what condition should we employ to the while loop so that the function correctly return the output ?
1
1
the while loop dont encounter at fun(0) and return zero to previous function fun(1) return zero to fun(2) this process continues  ,at last fun(8) receives zero
0
0

Thank you @benarji_2001

0
0