C Program to Find Factorial of a Number (4 methods)

By | August 24, 2020

In this article, you will learn to calculate the factorial of a number.

Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. For example factorial of 6 is 5*4*3*2*1 which is 120.

The factorial of a positive number n is given by:

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

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

Examples :

Input : 4
Output : 24

Input : 5
Output : 120 

Factorial can be calculated using following recursive formula.

n! = n * (n-1)!
n! = 1 if n = 0 or n = 1 

Implementation :

1. Using loop (Iterative) method :

// iterative method to find factorial of a number
#include <stdio.h>
 
 // driver
int main()
{
    // take input
    int num = 5;
  
    //product of numbers from num to 1
    int factorial = 1;
    for(int i=num; i>=1; i--)
    
        // multiply in increasing order
        factorial=factorial*i;

    // call factorial function and print factorial         
    printf("Factorial of given number is %d", factorial);     
    
    // successful completion  
    return 0;
}

Output :

Factorial of given number is 120 

2. Using Recursion :

// C program to find factorial of given number 
#include <stdio.h> 
  
// function to find factorial of given number 
unsigned int factorial(unsigned int n) 
{ 
    // for factorial of 0
    if (n == 0) return 1; 
    
    // factorial, for more than,  call recursive (n-1)
    return n * factorial(n - 1); 
} 
  
// driver program
int main() 
{ 
    // take input
    int num = 5; 
    // call factorial and print value of its factorial
    printf("Factorial of given number is %d", factorial(num));
    
    // successful completion
    return 0; 
} 

Output :

Factorial of given number is 120 

3. Using Ternary operator :

// program to find factorial using ternary operator  
#include <iostream> 
  
  // call function factorial
int factorial(int n) 
{ 
    // single line to find factorial 
    return (n == 1 || n == 0) 
               ? 1 
               : n * factorial(n - 1); 
} 
  
// Driver code 
int main() 
{ 
    // take input
    int num = 5; 
    
    // call factorial function and print factorial
    printf("Factorial of given number is %d", factorial(num)); 
    
    // successful completion
    return 0; 
} 

Output :

Factorial of given number is 120 

4. Using tgamma() method :
Note that tgamma(n+1)=n! , and tgamma is defined in math.h library of C.

// find factorial using tgamma() method
// tgamma() is defined in math.h
#include <math.h> 
// for standard input and output
#include <stdio.h> 
  
// driver program  
int main() 
{ 
    // take input
    int n = 5; 
  
    // tgamma(n+1)=n!
    // store the vaule of tgamma
    n = tgamma(n + 1); 
  
    // call factorial function and print factorial
    printf("Factorial of given number is %d", n); 
    
    // successful completion
    return 0; 
} 

Output :

Factorial of given number is 120 



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.