The Wayback Machine - https://web.archive.org/web/20240805051957/https://www.geeksforgeeks.org/one-line-function-for-factorial-of-a-number/
Open In App

One line function for factorial of a number

Last Updated : 17 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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)
 



Similar Reads

Find the last digit when factorial of A divides factorial of B
We are given two numbers A and B such that B &gt;= A. We need to compute the last digit of this resulting F such that F = B!/A! where 1 = A, B &lt;= 10^18 (A and B are very large).Examples: Input : A = 2, B = 4Output : 2Explanation : A! = 2 and B! = 24. F = 24/2 = 12 --&gt; last digit = 2Input : 107 109Output : 2As we know, factorial function grows
10 min read
Level order traversal line by line | Set 3 (Using One Queue)
Given a Binary Tree, print the nodes level-wise, each level on a new line. Output: 1 2 3 4 5Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. We have discussed two solutions in the articles below. Print level order traversal line by line | Set 1 Level order traversal line by line | Set 2 (Using Two Queues)In this p
6 min read
Check if a given number is factorial of any number
Given a number n, the task is to determine whether n can be a factorial of some number x.Examples: Input: N = 24Output: YesExplanation: 4! = 24 Input: N = 25Output: No Approach: For a number to be a factorial of any number it must be divisible by all the numbers from 1 to that number. So in this approach We keep on dividing the number if it is comp
4 min read
Slope of the line parallel to the line with the given slope
Given an integer m which is the slope of a line, the task is to find the slope of the line which is parallel to the given line. Examples: Input: m = 2 Output: 2 Input: m = -3 Output: -3 Approach: Let P and Q be two parallel lines with equations y = m1x + b1, and y = m2x + b2 respectively. Here m1 and m2 are the slopes of the lines respectively. Now
3 min read
Equation of straight line passing through a given point which bisects it into two equal line segments
Given a straight line which passes through a given point (x0, y0) such that this point bisects the line segment in two equal line segments. The task is to find the equation of this straight line.Examples: Input: x0 = 4, y0 = 3 Output: 3x + 4y = 24Input: x0 = 7, y0 = 12 Output: 12x + 7y = 168 Approach: Let PQ be the line and AB be the line segment b
4 min read
Equation of a straight line passing through a point and making a given angle with a given line
Given four integers a, b, c representing coefficients of a straight line with equation (ax + by + c = 0), the task is to find the equations of the two straight lines passing through a given point [Tex](x1, y1) [/Tex]and making an angle ? with the given straight line. Examples: Input: a = 2, b = 3, c = -7, x1 = 4, y1 = 9, ? = 30Output: y = -0.49x +1
15+ min read
Scan conversion of Line and Line Drawing algorithms
SCAN Conversion of Line : A line connects two points.It is a basic element in graphics.You'll need two spots between which to draw a line to draw a line . A line, or line segment, can be uniquely described by two points, according to geometry. We also know from algebra that a line can be defined by a slope, commonly denoted by the letter m, and a y
4 min read
Print level order traversal line by line | Set 1
Given root of a binary tree, The task is to print level order traversal in a way that nodes of all levels are printed in separate lines. Examples: Input: Output: 208 224 1210 14 Input: 1 / \ 2 3 / \ 4 5 Output: 12 34 5 Recommended PracticeLevel order traversal Line by LineTry It! Note: that this is different from simple level order traversal where
13 min read
Level order traversal line by line | Set 2 (Using Two Queues)
Given a Binary Tree, print the nodes level wise, each level on a new line. Output: 1 2 3 4 5Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. We have discussed one solution in below article. Print level order traversal line by line | Set 1 In this post, a different approach using two queues is discussed. We can ins
7 min read
First digit in factorial of a number
Given a positive integer n, find the first digit in its factorial. Examples : Input : n = 5 Output : 1 Factorial of 5 is 120 and first digit is 1. Input : 1000 Output : 4 A simple solution is to compute factorial of number, then find first digit in it. The above solution causes overflow soon. A better solution is to use the fact that factorial cont
5 min read
three90RightbarBannerImg