Python Program for Double Factorial

In the previous article, we have discussed Python Program for Product of Maximum in First array and Minimum in Second
Factorial:

The product of all positive integers less than or equal to n is the factorial of a non-negative integer n, denoted by n! in mathematics:

n! = n * (n – 1) *(n – 2) * . . . . . . . . . . 3 * 2 * 1.

4 != 4 * 3 * 2 *1=24

Double Factorial:

The product of all the integers from 1 to n with the same parity (odd or even) as n is the double factorial of a non-negative integer n. It is also known as a number’s semi factorial and is denoted by!!.

For example, the double factorial of 7 is 7*5*3*1 = 105

It is worth noting that as a result of this definition, 0!! = 1.

The double factorial for even number ‘n’ is:

The double factorial for odd number ‘n’ is:

Given a number ‘n’ and the task is to find the double factorial of a given number.

Examples:

Example 1:

Input:

Given number = 5

Output:

The double factorial of a given number{ 5 } =  15

Example 2:

Input:

Given number = 10

Output:

The double factorial of a given number{ 10 } =  3840

Program for Double Factorial in Python

Below are the ways to find the double factorial of a given number.

Method #1: Using For Loop (Static Input)

Approach:

• Give the number as static input and store it in a variable.
• Take a variable, initialize it with the value ‘1’, and store it in another variable say ‘c’.
• Loop from the given number to 0 in decreasing order with the stepsize of -2 using the for loop.
• Inside the loop multiply the above initialized variable c with the iterator value and store it in the same variable ‘c’.
• Print the double factorial of a given number.
• The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
numbr = 8
# Take a variable, initialize it with the value '1', and store it in another
# variable say 'c'.
c = 1
# Loop from the given number to 0 in decreasing order with the stepsize of -2
# using the for loop.
for i in range(numbr, 0, -2):
# Inside the loop multiply the above initialized variable c with the iterator value
# and store it in the same variable 'c'.
c *= i
# Print the double factorial of a given number.
print("The double factorial of a given number{", numbr, "} = ", c)


Output:

The double factorial of a given number{ 8 } =  384

Method #2: Using For loop (User Input)

Approach:

• Give the number as user input using the int(input()) function and store it in a variable.
• Take a variable, initialize it with the value ‘1’, and store it in another variable say ‘c’.
• Loop from the given number to 0 in decreasing order with the stepsize of -2 using the for loop.
• Inside the loop multiply the above initialized variable c with the iterator value and store it in the same variable ‘c’.
• Print the double factorial of a given number.
• The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
numbr = int(input("Enter a random number = "))
# Take a variable, initialize it with the value '1', and store it in another
# variable say 'c'.
c = 1
# Loop from the given number to 0 in decreasing order with the stepsize of -2
# using the for loop.
for i in range(numbr, 0, -2):
# Inside the loop multiply the above initialized variable c with the iterator value
# and store it in the same variable 'c'.
c *= i
# Print the double factorial of a given number.
print("The double factorial of a given number{", numbr, "} = ", c)


Output:

Enter a random number = 7
The double factorial of a given number{ 7 } = 105

Explore more instances related to python concepts fromÂ Python Programming ExamplesÂ Guide and get promoted from beginner to professional programmer level in Python Programming Language.