View All C Programs

 Previews Example  View All C Programs Next Example 

Program to Calculate Factorial of a Number Using Recursion in C

This program takes a positive integer from user and calculates the factorial of that number. Instead of loops to calculate factorial, this program uses recursive function to calculate the factorial of a number.
/* Source code to find factorial of a number. */

#include<stdio.h>
int factorial(int n);
int main()
{
    int n;
    printf("Enter an positive integer: ");
    scanf("%d",&n);
    printf("Factorial of %d = %ld", n, factorial(n));
    return 0;
}
int factorial(int n)
{
    if(n!=1)
     return n*factorial(n-1);
}
Result
Enter an positive integer: 5
Factorial of 5 = 120