Factorial in C

A factorial is a function that multiplies a whole number by every number below it.

For example:

6! = 6 x 5 x 4 x 3 x 2 x 1 = 720
720 is the factorial of 6.

4! = 4 x 3 x 2 x 1 = 24
24 is the factorial of 4.

Below code is the program for factorial in C using recursive function.  A function that calls itself is known as a recursive function.

#include<stdio.h>
#include<conio.h>
long int factorial(long int);
void main()
{
long int f,n;
clrscr();
printf("\n\n\nEnter the number: ");
scanf("%ld",&n);
f=factorial(n);
printf("\n\nFactorial of %ld is %ld",n,f);
getch();
}
long int factorial(long int n)
{
long int fact;
 if(n==1)
  return 1;
 else
  fact=n*factorial(n-1);
 return fact;
}

In the above program, factorial() is the recursive function and its call itself in if..else part.

Leave a Reply