Python Program to Compute 1/N!

In the previous article, we have discussed Python Program to Calculate the Surface Area and Volume of a Hemisphere
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

Given a number N and the task is to calculate the value of 1/N!

Examples:

Example1:

Input:

Given Number = 4

Output:

The value of 1/n! with given n value{ 4 } = 0.041666666666666664

Example2:

Input:

Given Number = 6

Output:

The value of 1/n! with given n value{ 6 } = 0.001388888888888889

Program to Compute 1/N! in Python

Below are the ways to calculate the value of 1/N! :

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 ‘factorl‘.
• Loop from 1 to the given number using the for loop.
• Inside the loop, calculate the factorial of a given number by multiplying the value of the above variable “factorl” with the iterator value.
• Store it in the same variable ‘factorl‘.
• Calculate the value of 1/factorl and store it in another variable.
• Print the value of 1/N! for the given number.
• The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gven_nval = 4
# Take a variable, initialize it with the value '1', and store it in another variable say
# 'factorl'.
factorl = 1
# Loop from 1 to the given number using the for loop.
for itr in range(1, gven_nval+1):
# Inside the loop, calculate the factorial of a given number by multiplying the value of
# the above variable "factorl" with the iterator value.
# Store it in the same variable 'factorl'.
factorl *= itr
# Calculate the value of 1/factorl and store it in another variable.
fnl_reslt = 1.0/factorl
# Print the value of 1/N! for the given number.
print("The value of 1/n! with given n value{", gven_nval, "} =", fnl_reslt)


Output:

The value of 1/n! with given n value{ 4 } = 0.041666666666666664

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 ‘factorl‘.
• Loop from 1 to the given number using the for loop.
• Inside the loop, calculate the factorial of a given number by multiplying the value of the above variable “factorl” with the iterator value.
• Store it in the same variable ‘factorl‘.
• Calculate the value of 1/factorl and store it in another variable.
• Print the value of 1/N! for the 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.
gven_nval = int(input("Enter some random Number = "))
# Take a variable, initialize it with the value '1', and store it in another variable say
# 'factorl'.
factorl = 1
# Loop from 1 to the given number using the for loop.
for itr in range(1, gven_nval+1):
# Inside the loop, calculate the factorial of a given number by multiplying the value of
# the above variable "factorl" with the iterator value.
# Store it in the same variable 'factorl'.
factorl *= itr
# Calculate the value of 1/factorl and store it in another variable.
fnl_reslt = 1.0/factorl
# Print the value of 1/N! for the given number.
print("The value of 1/n! with given n value{", gven_nval, "} =", fnl_reslt)


Output:

Enter some random Number = 6
The value of 1/n! with given n value{ 6 } = 0.001388888888888889

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.