Program to calculate the value of nPr Last Updated : 19 Mar, 2025 Summarize Comments Improve Suggest changes Share Like Article 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 Comment More infoAdvertise with us Next Article Program to calculate the value of nPr A arora_yash Follow Improve Article Tags : Java Mathematical Java Programs DSA permutation Permutation and Combination +2 More Practice Tags : JavaMathematicalpermutation Similar Reads Java Program to Calculate Simple Interest Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments. Simple interest formula is given by:Simple Interest = (P x T x R)/100 Where, P is 2 min read Java Program to Calculate Interest For FDs, RDs using Inheritance Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in java by which one class is allowed to inherit the features(fields and methods) of another class. In this article, we will create an application to calculate interest for FDs, RDs based on certain condition 6 min read C program to calculate the value of nPr nPr represents n permutation r and value of nPr is (n!) / (n-r)!. C #include<stdio.h> 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); } int main() { int n, r; printf("Enter n: "); scanf("%d", &n); printf 1 min read Program to find the Depreciation of Value The value of any article or item subject to wear and tear, decreases with time. This decrease is called its Depreciation. Given three variable V1, R and T where V1 is the initial value, R is the rate of depreciation and T is the time in years. The task is to find the value of the item after T years. 3 min read Program to calculate gross salary of a person Given an integer basic and a character grade which denotes the basic salary and grade of a person respectively, the task is to find the gross salary of the person. Gross Salary: The final salary computed after the additions of DA, HRA and other allowances. The formula for Gross Salary is defined as 5 min read Like