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

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Factorial of 5 is 120

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the last digit when factorial of A divides factorial of B
- Slope of the line parallel to the line with the given slope
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Check if a given number is factorial of any number
- Find the other end point of a line with given one end and mid
- Count trailing zeroes in factorial of a number
- Factorial of a large number
- Find the first natural number whose factorial is divisible by x
- Smallest number with at least n trailing zeroes in factorial
- Smallest number with at least n digits in factorial
- Program for factorial of a number
- Sum of divisors of factorial of a number
- Find sum of digits in factorial of a number
- Smallest number S such that N is a factor of S factorial or S!
- Find maximum power of a number that divides a factorial
- C Program for factorial of a number
- Java Program for factorial of a number
- Python Program for factorial of a number
- First digit in factorial of a number
- Factorial of a number in PL/SQL

This article is contributed by **Rithika palaniswamy**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.