C Program to Find Factorial of a Number Using Recursion

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


The factorial of a positive number n is given by:

factorial of n (n!) = 1 * 2 * 3 * 4 *...  * n

The factorial of a negative number doesn't exist. And the factorial of 0 is 1.

You will learn to find the factorial of a number using recursion in this example. Visit this page to learn how you can find the factorial of a number using a loop.


Factorial of a Number Using Recursion

#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
    int n;
    printf("Enter a positive integer: ");
    scanf("%d",&n);
    printf("Factorial of %d = %ld", n, multiplyNumbers(n));
    return 0;
}

long int multiplyNumbers(int n) {
    if (n>=1)
        return n*multiplyNumbers(n-1);
    else
        return 1;
}

Output

Enter a positive integer: 6
Factorial of 6 = 720

Suppose the user entered 6.

Initially, multiplyNumbers() is called from main() with 6 passed as an argument.

Then, 5 is passed to multiplyNumbers() from the same function (recursive call). In each recursive call, the value of argument n is decreased by 1.

When the value of n is less than 1, there is no recursive call and the factorial is returned ultimately to the main() function.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges