**Double factorial** of a non-negative integer n, is the product of all the integers from 1 to n that have the same parity (odd or even) as n. It is also called as **semifactorial** of a number and is denoted by **!!**. For example, double factorial of 9 is 9*7*5*3*1 which is 945. Note that, a consequence of this definition is 0!! = 1.

**Examples:**

Input:6Output:48 Note that 6*4*2 = 48Input:7Output:105 Note that 7*5*3 = 105

For even n, the double factorial is:

For odd n, the double factorial is:

**Recursive Solution:**

Double factorial can be calculated using following recursive formula.

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

Following is the implementation of double factorial.

## C++

`#include<stdio.h> ` ` ` `// function to find double factorial of given number ` `unsigned ` `int` `doublefactorial(unsigned ` `int` `n) ` `{ ` ` ` `if` `(n == 0 || n==1) ` ` ` `return` `1; ` ` ` `return` `n*doublefactorial(n-2); ` `} ` ` ` `int` `main() ` `{ ` ` ` `printf` `(` `"Double factorial is %d"` `, doublefactorial(5)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find double factorial ` ` ` `// of given number ` ` ` `static` `long` `doublefactorial(` `long` `n) ` ` ` `{ ` ` ` `if` `(n == ` `0` `|| n==` `1` `) ` ` ` `return` `1` `; ` ` ` ` ` `return` `n * doublefactorial(n - ` `2` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `main (String[] args) ` ` ` `{ ` ` ` `System.out.println(` `"Double factorial"` ` ` `+ ` `" is "` `+ doublefactorial(` `5` `)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## Python3

`# function to find double ` `# factorial of given number ` `def` `doublefactorial(n): ` ` ` ` ` `if` `(n ` `=` `=` `0` `or` `n ` `=` `=` `1` `): ` ` ` `return` `1` `; ` ` ` `return` `n ` `*` `doublefactorial(n ` `-` `2` `); ` ` ` `# Driver Code ` `print` `(` `"Double factorial is"` `, ` ` ` `doublefactorial(` `5` `)); ` ` ` `# This code is contributed ` `# by Smitha ` |

*chevron_right*

*filter_none*

## C#

`using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find double factorial ` ` ` `// of given number ` ` ` `static` `uint` `doublefactorial(` `uint` `n) ` ` ` `{ ` ` ` `if` `(n == 0 || n==1) ` ` ` `return` `1; ` ` ` ` ` `return` `n * doublefactorial(n - 2); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `Console.WriteLine(` `"Double factorial"` ` ` `+ ` `" is "` `+ doublefactorial(5)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code for ` `// Double factorial ` ` ` `// function return ` `// double factorial ` `function` `doublefactorial(` `$n` `) ` `{ ` ` ` `if` `(` `$n` `== 0 || ` `$n` `==1) ` ` ` `return` `1; ` ` ` `return` `$n` `* doublefactorial(` `$n` `- 2); ` `} ` ` ` ` ` `// Driver Code ` ` ` `echo` `"Double factorial is "` `, ` ` ` `doublefactorial(5); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

Double factorial is 15

**Iterative Solution:**

Double factorial can also be calculated iteratively as recursion can be costly for large numbers.

## C++

`#include<stdio.h> ` ` ` `// function to find double factorial of given number ` `unsigned ` `int` `doublefactorial(unsigned ` `int` `n) ` `{ ` ` ` `int` `res = 1; ` ` ` `for` `(` `int` `i=n; i>=0; i=i-2) ` ` ` `{ ` ` ` `if` `(i==0 || i==1) ` ` ` `return` `res; ` ` ` `else` ` ` `res *= i; ` ` ` `} ` `} ` ` ` `int` `main() ` `{ ` ` ` `printf` `(` `"Double factorial is %d"` `, doublefactorial(5)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find double factorial ` `// of given number ` `import` `java .io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find double factorial ` ` ` `// of given number ` ` ` `static` `int` `doublefactorial(` `int` `n) ` ` ` `{ ` ` ` `int` `res = ` `1` `; ` ` ` `for` `(` `int` `i = n; i >= ` `0` `; i = i-` `2` `) ` ` ` `{ ` ` ` `if` `(i == ` `0` `|| i == ` `1` `) ` ` ` `return` `res; ` ` ` `else` ` ` `res *= i; ` ` ` `} ` ` ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `System.out.println(` `"Double factorial"` ` ` `+ ` `" is "` `+ doublefactorial(` `5` `)); ` ` ` `} ` `} ` ` ` `// This code is Contributed by Anuj_67 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to find double ` `# factorial of given number ` ` ` `# Function to find double ` `# factorial of given number ` `def` `doublefactorial(n): ` ` ` `res ` `=` `1` `; ` ` ` `for` `i ` `in` `range` `(n, ` `-` `1` `, ` `-` `2` `): ` ` ` `if` `(i ` `=` `=` `0` `or` `i ` `=` `=` `1` `): ` ` ` `return` `res; ` ` ` `else` `: ` ` ` `res ` `*` `=` `i; ` ` ` `# Driver Code ` `print` `(` `"Double factorial is"` `, ` ` ` `doublefactorial(` `5` `)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find double factorial ` `// of given number ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find double factorial ` ` ` `// of given number ` ` ` `static` `int` `doublefactorial(` `int` `n) ` ` ` `{ ` ` ` `int` `res = 1; ` ` ` `for` `(` `int` `i = n; i >= 0; i = i-2) ` ` ` `{ ` ` ` `if` `(i == 0 || i == 1) ` ` ` `return` `res; ` ` ` `else` ` ` `res *= i; ` ` ` `} ` ` ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` `Console.Write(` `"Double factorial"` ` ` `+ ` `" is "` `+ doublefactorial(5)); ` ` ` `} ` `} ` ` ` `// This code is Contributed by Anuj_67 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` ` ` `// function to find double ` `// factorial of given number ` `function` `doublefactorial( ` `$n` `) ` `{ ` ` ` `$res` `= 1; ` ` ` `for` `(` `$i` `= ` `$n` `; ` `$i` `>= 0; ` `$i` `= ` `$i` `- 2) ` ` ` `{ ` ` ` `if` `(` `$i` `== 0 ` `or` `$i` `== 1) ` ` ` `return` `$res` `; ` ` ` `else` ` ` `$res` `*= ` `$i` `; ` ` ` `} ` `} ` ` ` ` ` `// Driver Code ` ` ` `echo` `"Double factorial is "` `, doublefactorial(5); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Double factorial is 15

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

**Important Points :**

- Double factorial and factorial are related using below formula.
**Note :**n!! means double factorial. If n is even, i.e., n = 2k n!! = 2^{k}k! Else (n = 2k + 1) n!! = (2k)! / 2^{k}k! - Double factorial is frequently used in combinatorics. Refer wiki for list of applications. An example application is count of perfect matchings of a complete graph K
_{n+1}for odd n.

**References:**

https://en.wikipedia.org/wiki/Double_factorial

This article is contributed by **Rahul Agrawal**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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
- Count digits in a factorial | Set 2
- Count trailing zeroes in factorial of a number
- Factorial of a large number
- Count factorial numbers in a given range
- Count digits in a factorial | Set 1
- Count Divisors of Factorial
- Find the first natural number whose factorial is divisible by x
- Smallest number with at least n trailing zeroes in factorial
- Last non-zero digit of a factorial
- Smallest number with at least n digits in factorial
- Program for factorial of a number
- Maximum value of an integer for which factorial can be calculated on a machine
- 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 all factorial numbers less than or equal to n
- Largest power of k in n! (factorial) where k may not be prime
- One line function for factorial of a number
- Factorial of each element in Fibonacci series