Hii friends
In this example,you will learn about how to find factorial of a given(positive) number in c language using recurssion.
For example-
Sample input
5
Sample output
Factorial of 5 is 120.
PROGRAM
YOU CAN COPY THE CODE FROM HERE
in this code we wrote a function named factorial.in that we write our program, if n >=1 code returns n*factorial(n-1).and this factorial(n-1) again goes to the function as n-1 and it will repeat it until n becomes 0;(becuase we wrote n>=1 in if condition).if n=0 then the fuction return 1.
for example
if we take n=5.
in the function it goes to if condition and check the condition.
if(5>=1) true
then it returns 5*factorial(5-1);
5*factorial(4);
then it returns 5*4*factorial(4-1); == 5*4*factorial(3); (if condition true).
then it returns 5*4*3*factorial(3-1); == 5*4*factorial(2); (if condition true).
then it returns 5*4*3*2factorial(2-1); == 5*4*factorial(1); (if condition true).
then it returns 5*4*3*2*factorial(1-1); == 5*4*factorial(0); (if condition false).
here if condition false then it returns 1;
so it becomes 5*4*3*2*1==120.
in this way this this function recall itself until the if condition false.
THANK YOU
<script src="//onlinegdb.com/embed/js/HxH3OnO
ReplyDeletehttps://onlinegdb.com/dOY2hiCOq
ReplyDeletehttps://onlinegdb.com/D9cn8LDzf
ReplyDeletehttps://onlinegdb.com/g2LUGRthA
ReplyDelete