# C Program to find sum of two numbers

#include<stdio.h>

#include<conio.h>

int number,factorial;

clrscr();

printf("Enter a number:");

scanf("%d",&number);

factorial=facto(number);

printf("Factorial of number %d is:%d",number,factorial);

getch();

}

int facto(int number)

{

if(number==0)

return 1;

return(number*facto(number-1));

}

#include<conio.h>

int number,factorial;

clrscr();

printf("Enter a number:");

scanf("%d",&number);

factorial=facto(number);

printf("Factorial of number %d is:%d",number,factorial);

getch();

}

int facto(int number)

{

if(number==0)

return 1;

return(number*facto(number-1));

}

## Explanation:

The above program will find out the factorial of a given number by using recursion. The process of calculating factorial by using recursion is very confusing. The program takes an integer value from user and assigns that value in the variable 'number'. This number is passed as an argument to the function facto for calculating the factorial of the given number. In the facto function we are calculating the factorial of given number by recursively calling the function again and again until the value of number become equal to 0.We can understand this process with the following example:

number=3

Function facto() will call with argument number as:facto(number)

Now number =3 so function will return the value 3*facto(2).The value of facto(2) will get save in the stack.

Now function facto() will recursively call itself with argument number=2

Now number=2 so function will return the value 2*facto(1).The value of facto(1) will get save in the stack.

Now function facto() will again call itself with argument number=1

Now number=1 so function will return the value 1*facto(0).The value of facto(0) will get save in the stack.

Now the number=0 ,so program will return 1

Now as we know the value of facto(0)=1 is pop from the stack so facto(0)*1=1

The value of facto(0)=1 is pop from stack so facto(0)*1=1

The value of facto(1)=1 is pop from stack so facto(1)*2=2

The value of facto(2)=2 is pop from the stack so facto(2)*3=6

So factorial(3)=6

The output of the above program is given below:Function facto() will call with argument number as:facto(number)

Now number =3 so function will return the value 3*facto(2).The value of facto(2) will get save in the stack.

Now function facto() will recursively call itself with argument number=2

Now number=2 so function will return the value 2*facto(1).The value of facto(1) will get save in the stack.

Now function facto() will again call itself with argument number=1

Now number=1 so function will return the value 1*facto(0).The value of facto(0) will get save in the stack.

Now the number=0 ,so program will return 1

Now as we know the value of facto(0)=1 is pop from the stack so facto(0)*1=1

The value of facto(0)=1 is pop from stack so facto(0)*1=1

The value of facto(1)=1 is pop from stack so facto(1)*2=2

The value of facto(2)=2 is pop from the stack so facto(2)*3=6

So factorial(3)=6