Program to calculate the value of nPr Last Updated : 19 Mar, 2025 Comments Improve Suggest changes 5 Likes Like Report Try it on GfG Practice Given two numbers, n and r, the task is to compute nPr, which represents the number of ways to arrange r elements from a set of n elements. It is calculated using the formula n!/(n−r)!, where "!" denotes the factorial operation.nPr = n! / (n - r)! Examples:Input: n = 5 r = 2Output: 20Explanation: 5P2 = 5! / (5 - 2)! = 20Input: n = 6 r = 3Output: 120Explanation: 6P3 = 6! / (6 - 3)! = 120Approach: Using Iterative Factorial Method - O(n) time and O(1) spaceThis approach calculates nPr using an iterative factorial function. It first computes n! and (n - r)! separately and then divides them to get the result. The factorial function uses a loop to multiply numbers from 1 to n. C++ // C++ program to calculate nPr using iteration #include <iostream> using namespace std; // Function to calculate factorial long long fact(int n) { long long result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; } // Function to calculate nPr long long 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) << endl; return 0; } C // C program to calculate nPr using iteration #include <stdio.h> // Function to calculate factorial long long fact(int n) { long long result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; } // Function to calculate nPr long long nPr(int n, int r) { return fact(n) / fact(n - r); } // Driver code int main() { int n = 5; int r = 2; printf("%dP%d = %lld\n", n, r, nPr(n, r)); return 0; } Java // Java program to calculate nPr using iteration public class GfG { // Function to calculate factorial static long fact(int n) { long result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; } // Function to calculate nPr static long nPr(int n, int r) { return fact(n) / fact(n - r); } // Driver code public static void main(String[] args) { int n = 5; int r = 2; System.out.println(n + "P" + r + " = " + nPr(n, r)); } } Python # Python program to calculate nPr using iteration # Function to calculate factorial def fact(n): result = 1 for i in range(2, n + 1): result *= i return result # Function to calculate nPr def nPr(n, r): return fact(n) // fact(n - r) # Driver code n = 5 r = 2 print(f'{n}P{r} = {nPr(n, r)}') C# // C# program to calculate nPr using iteration using System; class GfG { // Function to calculate factorial static long Fact(int n) { long result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; } // Function to calculate nPr static long nPr(int n, int r) { return Fact(n) / Fact(n - r); } // Driver code static void Main() { int n = 5; int r = 2; Console.WriteLine(n + "P" + r + " = " + nPr(n, r)); } } JavaScript // Function to calculate factorial function fact(n) { let result = 1; for (let i = 2; i <= n; i++) { result *= i; } return result; } // Function to calculate nPr function nPr(n, r) { return fact(n) / fact(n - r); } // Driver code const n = 5; const r = 2; console.log(`${n}P${r} = ${nPr(n, r)}`); Output5P2 = 20 Create Quiz Comment A arora_yash Follow 5 Improve A arora_yash Follow 5 Improve Article Tags : DSA permutation Permutation and Combination Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 3 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like