Program to print nCr using While loop
- Write a program to print nCr using While loop in C
- Write a program to print nCr using While loop in C++
- Write a program to print nCr using While loop in Python
- Write a program to print nCr using While loop in PHP
- Write a program to print nCr using While loop in Java
- Write a program to print nCr using While loop in Java Script
- Write a program to print nCr using While loop in C#
Explanation:
Combinations are methods of choosing elements from a collection in which the order is irrelevant. In contrast to permutations, combinations do not depend on the order of the items.
Formula
nCr = n! / r!(n−r)!
Where:
- n! (n factorial) is the product of all positive integers from 1 to n.
- r! is the factorial of r.
- (n−r)! is the factorial of n−r.
Program to print nCr using While loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#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);
// Calculate n!
int i = 1;
while (i <= n) {
fact_n *= i;
i++;
}
// Calculate r!
i = 1;
while (i <= r) {
fact_r *= i;
i++;
}
// Calculate (n-r)!
i = 1;
while (i <= (n - r)) {
fact_nr *= i;
i++;
}
// Calculate nCr
int nCr = fact_n / (fact_r * fact_nr);
printf("nCr = %d\n", nCr);
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;
// Calculate n!
int i = 1;
while (i <= n) {
fact_n *= i;
i++;
}
// Calculate r!
i = 1;
while (i <= r) {
fact_r *= i;
i++;
}
// Calculate (n-r)!
i = 1;
while (i <= (n - r)) {
fact_nr *= i;
i++;
}
// Calculate nCr
int nCr = fact_n / (fact_r * fact_nr);
cout << "nCr = " << nCr << 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}")
<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-python">n = int(input("Enter value of n: "))
r = int(input("Enter value of r: "))
fact_n, fact_r, fact_nr = 1, 1, 1
# Calculate n!
i = 1
while i <= n:
fact_n *= i
i += 1
# Calculate r!
i = 1
while i <= r:
fact_r *= i
i += 1
# Calculate (n-r)!
i = 1
while i <= (n - r):
fact_nr *= i
i += 1
# Calculate nCr
nCr = fact_n // (fact_r * fact_nr)
print(f"nCr = {nCr}")
PHP
<pre class="!overflow-visible"><div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none">php<div class="sticky top-9 md:top-[5.75rem]"><div class="absolute bottom-0 right-2 flex h-9 items-center"><div class="flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary"><button class="flex gap-1 items-center select-none py-1" aria-label="Copy"><svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="icon-sm"><path fill-rule="evenodd" clip-rule="evenodd" d="M7 5C7 3.34315 8.34315 2 10 2H19C20.6569 2 22 3.34315 22 5V14C22 15.6569 20.6569 17 19 17H17V19C17 20.6569 15.6569 22 14 22H5C3.34315 22 2 20.6569 2 19V10C2 8.34315 3.34315 7 5 7H7V5ZM9 7H14C15.6569 7 17 8.34315 17 10V15H19C19.5523 15 20 14.5523 20 14V5C20 4.44772 19.5523 4 19 4H10C9.44772 4 9 4.44772 9 5V7ZM5 9C4.44772 9 4 9.44772 4 10V19C4 19.5523 4.44772 20 5 20H14C14.5523 20 15 19.5523 15 19V10C15 9.44772 14.5523 9 14 9H5Z" fill="currentColor">Copy code<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><?php
$n = (int)readline("Enter value of n: ");
$r = (int)readline("Enter value of r: ");
$fact_n = 1;
$fact_r = 1;
$fact_nr = 1;
// Calculate n!
$i = 1;
while ($i <= $n) {
$fact_n *= $i;
$i++;
}
// Calculate r!
$i = 1;
while ($i <= $r) {
$fact_r *= $i;
$i++;
}
// Calculate (n-r)!
$i = 1;
while ($i <= ($n - $r)) {
$fact_nr *= $i;
$i++;
}
// Calculate nCr
$nCr = $fact_n / ($fact_r * $fact_nr);
echo "nCr = $nCr\n";
?>
import java.util.Scanner;
public class NCr {
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;
// Calculate n!
while (i <= n) {
fact_n *= i;
i++;
}
// Calculate r!
i = 1;
while (i <= r) {
fact_r *= i;
i++;
}
// Calculate (n-r)!
i = 1;
while (i <= (n - r)) {
fact_nr *= i;
i++;
}
// Calculate nCr
int nCr = fact_n / (fact_r * fact_nr);
System.out.println("nCr = " + nCr);
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;
// Calculate n!
let i = 1;
while (i <= n) {
fact_n *= i;
i++;
}
// Calculate r!
i = 1;
while (i <= r) {
fact_r *= i;
i++;
}
// Calculate (n-r)!
i = 1;
while (i <= (n - r)) {
fact_nr *= i;
i++;
}
// Calculate nCr
let nCr = fact_n / (fact_r * fact_nr);
console.log(`nCr = ${nCr}`);
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;
// Calculate n!
int i = 1;
while (i <= n) {
fact_n *= i;
i++;
}
// Calculate r!
i = 1;
while (i <= r) {
fact_r *= i;
i++;
}
// Calculate (n-r)!
i = 1;
while (i <= (n - r)) {
fact_nr *= i;
i++;
}
// Calculate nCr
int nCr = fact_n / (fact_r * fact_nr);
Console.WriteLine($"nCr = {nCr}");
}
}