In the previous article, we have discussed Python Program to Compute the Area and Perimeter of Pentagon
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.
5 != 5* 4 * 3 * 2 *1= 120
Given the values of x, n and the task is to calculate the value of x^n/n!
Examples:
Example1:
Input:
Given Number = 3 Given x value = 6
Output:
The value of x^n/n! with the given n,x { 3 6 } values= 36.0Example2:
Input:
Given Number = 5 Given x value = 8
Output:
The value of x^n/n! with the given n,x { 5 8 } values= 273.06666666666666Program to Compute x^n/n! in Python
Below are the ways to calculate the value of x^n/n! :
Method #1: Using For Loop (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Give the value of x as static input and store it in another 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 x^n/n! using the pow() function and store it in another variable.
- Print the value of x^n/n! for the given n, x values.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable.
gvn_n_val = 3
# Give the value of x as static input and store it in another variable.
gvn_x_val = 6
# 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, gvn_n_val+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 x^n/n! using the pow() function and store it in another variable.
fnl_reslt = pow(gvn_x_val, gvn_n_val)/factorl
# Print the value of x^n/n! for the given n, x values.
print("The value of x^n/n! with the given n,x {",
gvn_n_val, gvn_x_val, "} values=", fnl_reslt)
Output:
The value of x^n/n! with the given n,x { 3 6 } values= 36.0Method #2: Using For loop (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Give the value of x as user input using the int(input()) function and store it in another 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 x^n/n! using the pow() function and store it in another variable.
- Print the value of x^n/n! for the given n, x values.
- 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.
gvn_n_val = int(input("Enter some random Number= "))
# Give the value of x as user input using the int(input()) function and
# store it in another variable.
gvn_x_val = 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, gvn_n_val+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 x^n/n! using the pow() function and store it in another variable.
fnl_reslt = pow(gvn_x_val, gvn_n_val)/factorl
# Print the value of x^n/n! for the given n, x values.
print("The value of x^n/n! with the given n,x {",
gvn_n_val, gvn_x_val, "} values=", fnl_reslt)
Output:
Enter some random Number= 2
Enter some random Number= 10
The value of x^n/n! with the given n,x { 2 10 } values= 50.0Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
