Sunday, 19 February 2017

How Factorial Program Work

Algorithm
fact(int n)
  { if(n==1)
        return 1;
     else 
    return (n*fact(n-1));
}

lets manually run a program
1.lets take n=3;
2.so the if condition will get false and the program will got to else condition.
3. in else part our n is 3 at present so when we move  on return(3*2) it will execute it and now the value of n is decreased to 2.
4.then again this step will goes on till n==1  and when after that the first condition that is if condition will get true and programe will execute then.

No comments:

Post a Comment