# Overview

You must know about finding factorial of a number. For instance

0! = 1

1! = 1 = 1

2! = 2 * 1 = 2

3! = 3 * 2 * 1 = 6

....

There can be number of algorithms (Different Routes to the final solution!) which can be used for finding factorial of a number. So, Can you write a program that...

1. Finds factorial of a number using Recursion by calling another function.
2. Uses no other function except main () and uses Recursion.

# Annotations

Recursion happens when a function calls itself. I've never called myself. I am afraid that I might answer the phone and then have to deal with psychiatric problems. However, C has no problems with functions calling them, other that the psychiatric problems it tends to cause programmers when confronted with a recursive function and the task of figuring out what’s going on :). Each time you call a function some data is put on the stack using RAM that isn't released until the function returns. Each function you call within a function puts more data on the stack locking up more RAM. They may fill the stack leading to often-fatal condition known as blowing your stack. Don’t be intimidated to write this program now. It will not blow until you end up in infinite loop.

# Solutions

Code tested on TC++ 3.0 Compiler.

## Solution 1

```#include <stdio.h>
#include <conio.h>
extern int  i=1,j=1;
int feb(int  n)
{
if(n == 0)
return 1;
else
{j = j &#42; n;}
feb(n&#45;1);
return 1;
}
void main()
{
int i;
clrscr();
printf("Enter number whose factorial is needed\n");
scanf("%d",&i);
feb(i);
printf("The factorial is %d\n",j);
getch();
}```

## Solution 2

```#include <stdio.h>
#include <conio.h>
extern int b=1,i = 5;// i stores the number whose factorial is to be found.
int main(int a)
{
//static int b = 1,i = 5;
clrscr();
if(i==1)
{
return 1;
}
else
{
b = b &#42; i;
main(&#45;&#45;i);
if(i==1)
{
printf("%d\n",b);
i++;//Used  here so that b is printed only once.
}
getch();
return b;
}
}```

# Sample Output

```Enter the number whose factorial is needed
10
The factorial is 24320```

# Explanation

Solution 1 to the first problem is very easy and straightforward. Each time feb function is called, a variable j is multiplied with a counter which decrements by 1. This counter variable basically controls the number of times, feb function should be called to get the factorial of a number. Extern variable are loaded at compile time of the program. Static variable could have been used here because they are also load time variables. Register and Automatic are run time variables i.e. they can be assigned values during runtime i.e. each time the function in which they are initialized, is called, they are re-initialized to the given values. Rather that storing the previous contents, they change their values. Solution 2 to the second problem uses the same concept of Recursion except that here no other function is used. This program has a disadvantage that Number cannot be taken as Input from the user rather we have to change its value in the code itself.

Non-profit Tax ID # 203478467