Program to print nPr using For loop
- Write a program to print nPr using For loop in C
- Write a program to print nPr using For loop in C++
- Write a program to print nPr using For loop in Python
- Write a program to print nPr using For loop in PHP
- Write a program to print nPr using For loop in Java
- Write a program to print nPr using For loop in Java Script
- Write a program to print nPr using For 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
- nPr calculates how many different ways you can arrange r items out of n total items.
- The order of items is significant in permutations.
- r must be less than or equal to n, as you cannot choose more items than are available.
Program to print nPr using For loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
int main() {
int n, r;
printf("Enter value of n: ");
scanf("%d", &n);
printf("Enter value of r: ");
scanf("%d", &r);
int nPr = factorial(n) / factorial(n - r);
printf("%dP%d = %d\n", n, r, nPr);
return 0;
}
#include <iostream>
using namespace std;
int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
int main() {
int n, r;
cout << "Enter value of n: ";
cin >> n;
cout << "Enter value of r: ";
cin >> r;
int nPr = factorial(n) / factorial(n - r);
cout << n << "P" << r << " = " << nPr << endl;
return 0;
}
def factorial(num):
fact = 1
for i in range(1, num + 1):
fact *= i
return fact
n = int(input("Enter value of n: "))
r = int(input("Enter value of r: "))
nPr = factorial(n) // factorial(n - r)
print(f"{n}P{r} = {nPr}")
<?php
function factorial($num) {
$fact = 1;
for ($i = 1; $i <= $num; $i++) {
$fact *= $i;
}
return $fact;
}
$n = (int)readline("Enter value of n: ");
$r = (int)readline("Enter value of r: ");
$nPr = factorial($n) / factorial($n - $r);
echo "$n P $r = $nPr\n";
?>
import java.util.Scanner;
public class NPr {
public static int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter value of n: ");
int n = scanner.nextInt();
System.out.print("Enter value of r: ");
int r = scanner.nextInt();
int nPr = factorial(n) / factorial(n - r);
System.out.println(n + "P" + r + " = " + nPr);
scanner.close();
}
}
function factorial(num) {
let fact = 1;
for (let i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
let n = parseInt(prompt("Enter value of n:"));
let r = parseInt(prompt("Enter value of r:"));
let nPr = factorial(n) / factorial(n - r);
console.log(`${n}P${r} = ${nPr}`);
using System;
class Program {
static int Factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
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 nPr = Factorial(n) / Factorial(n - r);
Console.WriteLine($"{n}P{r} = {nPr}");
}
}