Python Program to calculate the value of nPr

In this article, we will learn how to calculate the value of nPr in Python.

Where nPr represented n permutation r. Permutation refers to the process of organizing all the individuals of a given set to form a sequence.

The mathematical notation of nPr is n!/(n-r)!

Calculate the value of nPr in Python

Method 1:

1.Get the user input.

2. Create a function to calculate the factorial of the n.

3. Apply the formulae of nPr which is n!/(n-r)!.

4. Finally, return the result.

def fact(n):
    res = 1
    for i in range (1, n+1):
        res *= i
    return res
def nPr(n, r):
    return fact(n)/fact(n-r)
n = int(input("Enter the n value: "))
r = int(input("Enter the r value: "))
print("The nPr value is: ", nPr(n, r))

Output

Enter the n value: 5
Enter the r value: 2
The nPr value is: 20.0

Method 2:

Python has a math module that has an inbuilt factorial() function that helps to calculate the factorial.

1. Get the user input.

2. Apply, the formulae and use the factorial() function to calculate the factorial value.

3. Finally, return the result.

import math

def nPr(n, r):
    return math.factorial(n)/(math.factorial(n-r))
n = int(input("Enter the n value: "))
r = int(input("Enter the r value: "))
print("The nPr value is: ", nPr(n, r))

Output

Enter the n value: 10
Enter the r value: 2
The nPr value is: 90.0

Enter the n value: 3
Enter the r value: 2
The nPr value is: 6.0

Also, refer

Leave a Reply

Your email address will not be published. Required fields are marked *