# Program to calculate the value of nPr

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.

^{n}P_{r}= n! / (n - r)!

**Program:**

## C++

`// 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 code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `int` `r = 2; ` ` ` ` ` `cout << n << ` `"P"` `<< r << ` `" = "` `<< nPr(n, r); ` `} ` ` ` `// This code is contributed by ` `// Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Java

`// 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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to calculate nPr ` `import` `math ` `def` `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 code ` `n ` `=` `5` `r ` `=` `2` ` ` `print` `(n, ` `"P"` `, r, ` `"="` `, nPr(n, r)) ` ` ` `# This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## C#

`// C# program to calculate nPr ` `using` `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 */` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to calculate nPr ` `function` `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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

5P2 = 20

Optimization for multiple queries of nPr

If there are multiple queries for nPr, we may precompute factorial values and use same for every call. This would avoid computation of same factorial values again and again.

## Recommended Posts:

- Java Program for Program to calculate area of a Tetrahedron
- Program to calculate value of nCr
- C program to calculate the value of nPr
- Program to calculate the value of nCr Efficiently
- Calculate MDAS Factorial of given number
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Java Program for Program to find area of a circle
- Java Program for Program for array rotation
- Program to Add Two Complex Numbers
- Java Program to take Screenshots
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Program to validate a user using JSP
- C Program for factorial of a number
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Java Program for Cutting a Rod | DP-13

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.