Example & Tutorial understanding programming in easy ways.

Factorial by the recursion in C language ?

Factorial:
-factorial of number is the multiplication of every integer number between 1 and that number.

Example-

factorial of 3 is 6(1*2*3)
factorial of 5 is 120(1*2*3*4*5)

Recursion:
-Function call itself is called as Recursion.
-We can also use the recursion concept at the place of the loop.

program-

#include < stdio.h>
int Factorial(int n)
{
if(n==1)
return 1;
else
return n*Factorial(n-1);
}
int main()
{
int n,ans;
printf("Enter Numbern");
scanf("%d",&n);
ans=Factorial(n);
printf("Factorial is %d",ans);
}


output-

Enter Number
5
Factorial is 120

-In this progrram, we call the function again and again with decreament value of n so that we multiple each number from 1 to n. and at last when n become 1, it return the 1 and call break. and we get the required factorial.




Read More →