Program for 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 6*5*4*3*2*1 which is 720.

factorial

Recursive Solution:
Factorial can be calculated using following recursive formula.

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

Following is implementation of factorial.



C

// C program to find factorial of given number
#include<stdio.h>
  
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    if (n == 0)
      return 1;
    return n*factorial(n-1);
}
  
int main()
{
    int num = 5;
    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}

Java

// Java program to find factorial of given number
class Test
{
    // method to find factorial of given number
    static int factorial(int n)
    {
        if (n == 0)
          return 1;
          
        return n*factorial(n-1);
    }
      
    // Driver method
    public static void main(String[] args) 
    {
        int num = 5;
        System.out.println("Factorial of "+ num + " is " + factorial(5));
    }
}

Python3

# Python 3 program to find 
# factorial of given number
def factorial(n):
      
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1); 
  
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
  
# This code is contributed by Smitha Dinesh Semwal

C#

// C# program to find factorial
// of given number
using System;
  
class Test
{
    // method to find factorial
    // of given number
    static int factorial(int n)
    {
        if (n == 0)
        return 1;
          
        return n*factorial(n-1);
    }
      
    // Driver method
    public static void Main() 
    {
        int num = 5;
        Console.WriteLine("Factorial of "
          + num + " is " + factorial(5));
    }
}
  
// This code is contributed by vt_m

PHP

<?php
// PHP program to find factorial
// of given number
  
// function to find factorial
// of given number
function factorial($n)
{
    if ($n == 0)
        return 1;
    return $n * factorial($n - 1);
}
  
    // Driver Code
    $num = 5;
    echo "Factorial of ",$num," is "
                 , factorial($num);
  
// This code is contributed by m_kit
?>

Output:

Factorial of 5 is 120

Iterative Solution:
Factorial can also be calculated iteratively as recursion can be costly for large numbers.

C

#include<stdio.h>
  
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    int res = 1, i;
    for (i=2; i<=n; i++)
        res *= i;
    return res;
}
  
int main()
{
    int num = 5;
    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}

Java

// Java program to find factorial of given number
class Test
{
    // Method to find factorial of given number
    static int factorial(int n)
    {
        int res = 1, i;
        for (i=2; i<=n; i++)
            res *= i;
        return res;
    }
      
    // Driver method
    public static void main(String[] args) 
    {
        int num = 5;
        System.out.println("Factorial of "+ num + " is " + factorial(5));
    }
}

Python3

# Python 3 program to find  
# factorial of given number
def factorial(n):
      
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1); 
  
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
  
# This code is contributed by Smitha Dinesh Semwal

C#

// C# program to find 
// factorial of given number
using System;
  
class Test
{
    // Method to find factorial
    // of given number
    static int factorial(int n)
    {
        int res = 1, i;
      
        for (i = 2; i <= n; i++)
            res *= i;
        return res;
    }
      
    // Driver method
    public static void Main() 
    {
        int num = 5;
        Console.WriteLine("Factorial of " +
               num + " is " + factorial(5));
    }
}
  
// This code is contributed by vt_m

PHP

<?php
  
// function to find factorial 
// of given number
function factorial( $n)
{
    $res = 1; $i;
    for ($i = 2; $i <= $n; $i++)
        $res *= $i;
    return $res;
}
  
// Driver Code
$num = 5;
echo "Factorial of ", $num, " is "
                    factorial($num);
  
// This code is contributed 
// by anuj_67.
?>

Output :

Factorial of 5 is 120

Time complexity of the above solutions is O(n).

One line Solution (Using Ternary operator):

C++

// C++ program to find factorial of given number
#include<iostream>
  
int factorial(int n)
{
    // single line to find factorial
    return (n==1 || n==0) ? 1: n * factorial(n - 1); 
}
  
// Driver Code
int main()
{
    int num = 5;
    printf ("Factorial of %d is %d", num, factorial(num));
    return 0;
}
  
// This code is contributed by  Rithika palaniswamy.

Java

// Java program to find factorial
// of given number
class Factorial {
  
 int factorial(int n)
  {
  
    // single line to find factorial
    return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
  
  }
  
  
  // Driver Code
  public static void main(String args[])     
  {
    Factorial obj = new Factorial();
    int num = 5;
    System.out.println("Factorial of " + num + 
                      " is " + obj.factorial(num));
   }
}
  
// This code is contributed by Anshika Goyal.

Python3

# Python 3 program to find
# factorial of given number
  
def factorial(n):
  
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1
  
  
# Driver Code
num = 5
print ("Factorial of",num,"is",
      factorial(num))
  
# This code is contributed
# by Smitha Dinesh Semwal.

C#

// C# program to find factorial
// of given number
using System;
  
class Factorial {
  
    int factorial(int n)
    {
      
        // single line to find factorial
        return (n == 1 || n == 0) ? 1 :
                        n * factorial(n - 1);
      
    }
      
    // Driver Code
    public static void Main() 
    {
        Factorial obj = new Factorial();
        int num = 5;
          
        Console.WriteLine("Factorial of " + num + 
                        " is " + obj.factorial(num));
    }
}
  
// This code is contributed by vt_m.

PHP

<?php
// PHP program to find factorial
// of given number
  
function factorial( $n)
{
      
    // single line to find factorial
    return ($n == 1 || $n == 0) ? 1: 
             $n * factorial($n - 1); 
}
  
    // Driver Code
    $num = 5;
    echo "Factorial of " , $num, " is ", factorial($num);
  
// This code is contributed by anuj_67.
?>

Output:

Factorial of 5 is 120

The above solutions cause overflow for small numbers. Please refer factorial of large number for a solution that works for large numbers.

Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.



My Personal Notes arrow_drop_up

Improved By : jit_t, vt_m




Practice Tags :

Recommended Posts:



1.5 Average Difficulty : 1.5/5.0
Based on 14 vote(s)






User Actions