Open In App

Program to calculate the value of nPr

Last Updated : 19 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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 = 2
Output: 20
Explanation: 5P2 = 5! / (5 - 2)! = 20

Input: n = 6
r = 3
Output: 120
Explanation: 6P3 = 6! / (6 - 3)! = 120

Approach: Using Iterative Factorial Method - O(n) time and O(1) space

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

Output
5P2 = 20

Next Article

Similar Reads