Program to print nPr using While loop

Program to print nPr using While loop

  • Write a program to print nPr using While loop in C
  • Write a program to print nPr using While loop in C++
  • Write a program to print nPr using While loop in Python
  • Write a program to print nPr using While loop in PHP
  • Write a program to print nPr using While loop in Java
  • Write a program to print nPr using While loop in Java Script
  • Write a program to print nPr using While loop in C#

Explanation:

An arrangement of elements in a particular order is called a permutation. In permutations, the arrangement’s order is important.

nPr = n! / (n−r)!

Where:

  • n! (n factorial) is the product of all positive integers from 1 to n.
  • (n−r)! is the factorial of n−r.

Key Points

  1. nPr calculates how many different ways you can arrange r items out of n total items.
  2. The order of items is significant in permutations.
  3. r must be less than or equal to n, as you cannot choose more items than are available.

Program to print nPr using While loop

#include <stdio.h>

int main() {
    int n, r, fact_n = 1, fact_r = 1, fact_nr = 1;

    printf("Enter value of n: ");
    scanf("%d", &n);
    printf("Enter value of r: ");
    scanf("%d", &r);

    int i = 1;
    while (i <= n) {
        fact_n *= i;
        i++;
    }

    i = 1;
    while (i <= r) {
        fact_r *= i;
        i++;
    }

    i = 1;
    while (i <= (n - r)) {
        fact_nr *= i;
        i++;
    }

    int nPr = fact_n / fact_nr;
    printf("nPr = %d\n", nPr);

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int n, r, fact_n = 1, fact_r = 1, fact_nr = 1;

    cout << "Enter value of n: ";
    cin >> n;
    cout << "Enter value of r: ";
    cin >> r;

    int i = 1;
    while (i <= n) {
        fact_n *= i;
        i++;
    }

    i = 1;
    while (i <= r) {
        fact_r *= i;
        i++;
    }

    i = 1;
    while (i <= (n - r)) {
        fact_nr *= i;
        i++;
    }

    int nPr = fact_n / fact_nr;
    cout << "nPr = " << nPr << endl;

    return 0;
}

n = int(input("Enter value of n: "))
r = int(input("Enter value of r: "))

fact_n, fact_r, fact_nr = 1, 1, 1

i = 1
while i <= n:
    fact_n *= i
    i += 1

i = 1
while i <= r:
    fact_r *= i
    i += 1

i = 1
while i <= (n - r):
    fact_nr *= i
    i += 1

nPr = fact_n // fact_nr
print(f"nPr = {nPr}")

<?php
$n = (int)readline("Enter value of n: ");
$r = (int)readline("Enter value of r: ");
$fact_n = 1;
$fact_r = 1;
$fact_nr = 1;

$i = 1;
while ($i <= $n) {
    $fact_n *= $i;
    $i++;
}

$i = 1;
while ($i <= $r) {
    $fact_r *= $i;
    $i++;
}

$i = 1;
while ($i <= ($n - $r)) {
    $fact_nr *= $i;
    $i++;
}

$nPr = $fact_n / $fact_nr;
echo "nPr = $nPr\n";
?>

import java.util.Scanner;

public class NPr {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter value of n: ");
        int n = sc.nextInt();
        System.out.print("Enter value of r: ");
        int r = sc.nextInt();

        int fact_n = 1, fact_r = 1, fact_nr = 1;
        int i = 1;

        while (i <= n) {
            fact_n *= i;
            i++;
        }

        i = 1;
        while (i <= r) {
            fact_r *= i;
            i++;
        }

        i = 1;
        while (i <= (n - r)) {
            fact_nr *= i;
            i++;
        }

        int nPr = fact_n / fact_nr;
        System.out.println("nPr = " + nPr);

        sc.close();
    }
}

let n = parseInt(prompt("Enter value of n: "));
let r = parseInt(prompt("Enter value of r: "));

let fact_n = 1, fact_r = 1, fact_nr = 1;

let i = 1;
while (i <= n) {
    fact_n *= i;
    i++;
}

i = 1;
while (i <= r) {
    fact_r *= i;
    i++;
}

i = 1;
while (i <= (n - r)) {
    fact_nr *= i;
    i++;
}

let nPr = fact_n / fact_nr;
console.log(`nPr = ${nPr}`);

using System;

class Program {
    static void Main() {
        Console.Write("Enter value of n: ");
        int n = int.Parse(Console.ReadLine());
        Console.Write("Enter value of r: ");
        int r = int.Parse(Console.ReadLine());

        int fact_n = 1, fact_r = 1, fact_nr = 1;

        int i = 1;
        while (i <= n) {
            fact_n *= i;
            i++;
        }

        i = 1;
        while (i <= r) {
            fact_r *= i;
            i++;
        }

        i = 1;
        while (i <= (n - r)) {
            fact_nr *= i;
            i++;
        }

        int nPr = fact_n / fact_nr;
        Console.WriteLine($"nPr = {nPr}");
    }
}

List of All Programs