Open In App
Related Articles

One line function for factorial of a number

Improve Article
Improve
Save Article
Save
Like Article
Like

Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. 
 

Example :

Factorial of 6 is 6 * 5 * 4 * 3 * 2 * 1 which is 720.

We can find the factorial of a number in one line with the help of Ternary operator or commonly known as Conditional operator in recursion.
 

 

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;
}


Java




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


Python3




# Python3 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 is contributed by mits


C#




// C# program to find factorial
// of given number
using System;
  
class GFG 
{
  
    // Function to calculate factorial
    static int factorial(int n)
    {
          
        // single line to find factorial
        return (n == 1 || n == 0) ?
                1 : n * factorial(n - 1);
    }
  
    public static void Main()
    {
          
        int num = 5;
        Console.WriteLine("Factorial of " + num + 
                        " is " + 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 Ajit.
?>


Javascript




<script>
// Javascript 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
let num = 5;
document.write("Factorial of ", num, 
     " is ", factorial(num));
  
// This code is contributed by _saurabh_jaiswal.
</script>


Output :

 Factorial of 5 is 120

Time complexity: O(n) where n is given number

Auxiliary Space: O(n)
 


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 17 Feb, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials