Program to calculate the value of nPr - GeeksforGeeks

07 May, 2021
Given two numbers n and r, the task is to find the value of nPr. nPr represents n permutation r which is calculated as n!/(n-k)!. Permutation refers to the process of arranging all the members of a given set to form a sequence. The number of permutations on a set of n elements is given by n! where “!” represents factorial. 
nPr = n! / (n - r)! 
Program:  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to calculate nPr#include<bits/stdc++.h>using namespace std; int fact(int n){    if (n <= 1)        return 1;    return n * fact(n - 1);} int nPr(int n, int r){    return fact(n) / fact(n - r);} // Driver codeint main(){    int n = 5;    int r = 2;     cout << n << "P" << r << " = " << nPr(n, r);} // This code is contributed by// Surendra_Gangwar
// Java program to calculate nPr import java.util.*; public class GFG {     static int fact(int n)    {        if (n <= 1)            return 1;        return n * fact(n - 1);    }     static int nPr(int n, int r)    {        return fact(n) / fact(n - r);    }     public static void main(String args[])    {        int n = 5;        int r = 2;         System.out.println(n + "P" + r + " = "                           + nPr(n, r));    }}
# Python3 program to calculate nPrimport mathdef fact(n):    if (n <= 1):        return 1             return n * fact(n - 1) def nPr(n, r):         return math.floor(fact(n) /                fact(n - r))     # Driver coden = 5r = 2 print(n, "P", r, "=", nPr(n, r)) # This code contributed by Rajput-Ji
// C# program to calculate nPrusing System; class GFG{     static int fact(int n)    {        if (n <= 1)            return 1;        return n * fact(n - 1);    }     static int nPr(int n, int r)    {        return fact(n) / fact(n - r);    }     public static void Main()    {        int n = 5;        int r = 2;         Console.WriteLine(n + "P" + r + " = "                        + nPr(n, r));    }} /* This code contributed by PrinciRaj1992 */
<?php// PHP program to calculate nPrfunction fact($n){    if ($n <= 1)        return 1;             return $n * fact($n - 1);} function nPr($n, $r){    return floor(fact($n) /                 fact($n - $r));}     // Driver code$n = 5;$r = 2; echo $n, "P", $r, " = ", nPr($n, $r); // This code is contributed by Ryuga?>
// Javascript program to calculate nPrfunction fact(n){    if (n <= 1)        return 1;             return n * fact(n - 1);} function nPr(n, r){    return Math.floor(fact(n) /                fact(n - r));}     // Driver codelet n = 5;let r = 2; document.write(n, "P", r, " = ", nPr(n, r)); // This code is contributed by gfgking
5P2 = 20
 
Optimization for multiple queries of nPr If there are multiple queries for nPr, we may precompute factorial values and use the same for every call. This would avoid the computation of the same factorial values again and again. 
princiraj1992
ankthon
Rajput-Ji
SURENDRA_GANGWAR
gfgking
factorial
Combinatorial
Java Programs
Combinatorial
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count of subsets with sum equal to X
Python program to get all subsets of given size of a set
Print all distinct permutations of a given string with duplicates
Heap's Algorithm for generating permutations
Find all distinct subsets of a given set using BitMasking Approach
Initializing a List in Java
Convert a String to Character Array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class