C Program to Compute Nth Factorial using Recursion

Factorial is a very important concept of mathematics and it’s used in counting numbers, probability and other areas of mathematics.

There are many ways to write a program for factorial, but for the sake of learning, we use two important way to program this problem.
The two ways to write factorial program are as follows.
  1. Program to compute nth factorial without using recursion.
  2. Program to compute nth factorial using recursion.
A recursion is an ability to a function or a procedure to call itself several times until the given problem is solved. 


Since you are programming using recursion, we will discuss more of it. A function calls itself by providing parameter one less than the original value for all subsequent calls.
It means if Add(n) is the original function with parameter n, then it calls itself with Add(n-1) or it totally depends on the problem. 
If you do not understand, do not worry – continue reading.
This program uses advanced concept and suitable for an intermediate level learner of C programming and it is written using Dev-C++ compiler version
In the following sections, you will find problem definition, flowchart, program code for practice and verified output of the program to help you learn faster.


Problem Definition
In simple words, the factorial of a given number n is the product of all the numbers up to that number, including the number.
So factorial of 5 is 1 x 2 x 3 x 4 x 5 = 120
You will be practicing the program for factorial with recursion. In the previous section, we introduced you to the concept of recursion.To elaborate on that see the example below.
If factorial is a function then,

Figure: Recursion

Every time factorial function calls itself, it provides one less of the parameter to the function until the factorial (1) is reached.
Then it starts working from factorial (1) to the top – factorial (n) and prints the result.


Flowchart: C Program to Compute Nth Factorial using Recursion


Program Code

/*Program to compute Nth factorial */

#include < stdio.h >
#include < stdlib.h >

int fact (int n)


unsigned int f;

if ((n == 0) || (n == 1))

return (n);


f = n * fact (n 1); /* Compute factorial by Recursive calls */

return (f);


main ()


int i, n;

/* Reading the number */

printf (“Enter the Number :”);

scanf (“%d”, & n);

/* Printing results */

for (i = 0; i < 30; i++)
printf (“_”);
printf (“nn”);

printf (“Factorial of Number %d is %dn”, n, fact (n));

for (i = 0; i < 30; i++)
printf (“_”);
printf (“nn”);

system (“PAUSE”);
return 0;



The output of the program is given below.

Output: C Program to Compute Nth Factorial using Recursion