Program to Calculate the Value of nPr

Python Program to Calculate the Value of nPr

In the previous article, we have discussed Python Program to Find the Number of Weeks between two Dates
nPr:

nPr indicates n permutation r.

The process of organizing all the individuals in a given set to form a sequence is referred to as permutation.

The Given mathematical formula for nPr =n!/(n-r)!

Given the values of n, r and the task is to find the value of nPr .

Examples:

Example1:

Input:

Given n value = 5 
Given r value = 4

Output:

The value of nPr for above given n, r values =  120

Example 2:

Input:

Given n value = 6
Given r value = 3

Output:

The value of nPr for above given n, r values = 120

Program to Calculate the Value of nPr

Below are the ways to calculate the value of nPr for given values of n,r.

Method #1: Using math Module (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the number as static input and store it in a variable.
  • Give another number as static input and store it in another variable.
  • Calculate the value of nPr with reference to the standard mathematical formula n!/(n-r)! using factorial() function
  • Store it in a variable.
  • Print the value of nPr for the above-given n, r values.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the number as static input and store it in a variable.
gvn_n_val = 5
# Give another number as static input and store it in another variable.
gvn_r_val = 4
# Calculate the value of nPr with reference to standard mathematical formula n!/(n-r)!
# using factorial() function 
# Store it in a variable.
n_p_r = math.factorial(gvn_n_val)//math.factorial(gvn_n_val-gvn_r_val)
# Print the value of nPr for the above given n, r values.
print("The value of nPr for above given n, r values = ", n_p_r)

Output:

The value of nPr for above given n, r values =  120

Method #2: Using math Module (User input)

Approach:

  • Import math module using the import keyword.
  • Give the number as User input using the int(input()) function and store it in a variable.
  • Give another number as User input using the int(input()) function and store it in another variable.
  • Calculate the value of nPr with reference to the standard mathematical formula n!/(n-r)! using factorial() function
  • Store it in a variable.
  • Print the value of nPr for the above-given n, r values.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# 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 another number as user input using the int(input()) function and store it in another variable.
gvn_r_val = int(input("Enter some Random number = "))
# Calculate the value of nPr with reference to standard mathematical formula n!/(n-r)!
# using factorial() function 
# Store it in a variable.
n_p_r = math.factorial(gvn_n_val)//math.factorial(gvn_n_val-gvn_r_val)
# Print the value of nPr for the above given n, r values.
print("The value of nPr for above given n, r values = ", n_p_r)

Output:

Enter some Random number = 6
Enter some Random number = 3
The value of nPr for above given n, r values = 120

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.