codescracker


python

Python Program to Find ncR and nPr



« Previous Program Next Program »


Find ncR and nPr in Python

To find ncR and nPr in python, you have to ask from user to enter the value of n and r and then find the value of ncR and nPr to print the result on output screen as shown in the given program.

Python Programming Code to Find ncR and nPr

Following python program ask from user to enter the value of n and r to find and print the value of ncR and nPr:

# Python Program - Find ncR and nPr

import math;
print("Enter 'x' for exit.");
nval = input("Enter value of n: ");
if nval == 'x':
    exit();
else:
    rval = input("Enter value of r: ");
    n = int(nval);
    r = int(rval);
    npr = math.factorial(n)/math.factorial(n-r);
    ncr = npr/math.factorial(r);
    print("ncR =",ncr);
    print("nPr =",npr);

Here is the sample run of the above Python program shows how to find ncR and nPr:

find ncr npr python

Supply the value of n say 7 and then r say 3, then press enter key to find out the value of ncR and nPr as shown in output given here:

print ncr npr python

Below is same program directly written and run on python shell:

python find ncR and nPr

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test