C Program to Find the Factorial of a Number using Recursion

This is a C Program to find the factorial of a number using recursion.

Problem Description

This C Program Finds the Factorial of a Number using Recursion.

Problem Solution

This C Program prints the factorial of a given number using recursion. A factorial is product of all the number from 1 to the user specified number.

Program/Source Code

Here is the source code of the C program to print the factorial of a given number. The C program is successfully compiled and run on
a Linux system. The program output is also shown below.

/*
 * C Program to find factorial of a given number using recursion
 */
#include <stdio.h>
 
int factorial(int);
 
int main()
{
    int num;
    int result;
 
    printf("Enter a number to find it's Factorial: ");
    scanf("%d", &num);
    if (num < 0)
    {
        printf("Factorial of negative number not possible\n");
    }
    else
    {
        result = factorial(num);
        printf("The Factorial of %d is %d.\n", num, result);
    }
    return 0;
}
int factorial(int num)
{
    if (num == 0 || num == 1)
    {
        return 1;
    }
    else
    {
        return(num * factorial(num - 1));
    }
}
Program Explanation

In this C Program, we are reading a number using ‘num’ variable. If else condition statement is used to check the value of ‘num’ variable is less than 0. If the condition is true, then execute the statement and print the factorial of negative number not possible.

advertisement
advertisement

Otherwise, if the condition is false, then execute the else statement, the factorial() function is used to find the factorial of a given number using recursion. A factorial is a product of all the number from 1 to the user specified number.

If else condition statement is used to check the ‘num’ variable value is equal to 0 or 1 using logical OR operator. If the condition is true then execute the statement and return value as 1.

Otherwise, if the condition is false, then execute the else statement. Multiply the value of ‘num’ variable with the value of ‘factorial() value and return the value. Print the factorial of a given number using printf statement.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
 
$ cc pgm5.c
$ a.out
Enter a number to find it's Factorial: 6
The Factorial of 6 is 720.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.