C program to Calculate Factorial of a Number Using Recursion

To understand this example, you should have knowledge of following C programming topics:

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 Calculate Factorial Using Recursion

/* Source code to find factorial of a number. */

int factorial(int n);
int main()
    int n;
    printf("Enter an positive integer: ");
    printf("Factorial of %d = %ld", n, factorial(n));
    return 0;
int factorial(int n)
     return n*factorial(n-1);


Enter an positive integer: 6
Factorial of 6 = 720