Comment calculer la valeur de nCr

La combinaison est un concept mathématique. Il fait référence à l'agencement de plusieurs objets où leur ordre n'a pas d'importance. Une formule de base décrit le nombre de combinaisons valides.

Dans cet article, vous apprendrez à calculer la valeur de nCr à l'aide de Python, C++, JavaScript, C et Java. Chaque exemple contient également un exemple de sortie pour plusieurs valeurs différentes.

Comment calculer nCr

Utilisez la formule de combinaison suivante pour calculer la valeur de nCr :

 nCr = n! / (r! * (nr)!)

Où:

 n = Total number
C = Combination
r = Arrangement
! = Factorial

Énoncé du problème

On vous donne les valeurs de n et r . Vous devez calculer la valeur de nCr .

Exemple 1 : Soit n = 10 et r = 5.

Par conséquent, nCr = 10 ! / (5! * (10-5)!) = 10! / (5! * 5!) = 252.

Ainsi, la sortie est de 252.

Exemple 2 : Soit n = 8 et r = 0.

Par conséquent, nCr = 8 ! / (0! * (8-0)!) = 8! / (0! * 8!) = 1.

Ainsi, la sortie est 1.

Programme C++ pour calculer la valeur de nCr

Ci-dessous se trouve le programme C++ pour calculer la valeur de nCr :

 // C++ program to calculate the value of nCr
#include <iostream>
using namespace std;
// Function to calculate the factorial of a number
int factorial(int num)
{
if (num<=1)
{
return 1;
}
return num*factorial(num-1);
}
// Function to calculate the value of nCr
int calculate_nCr(int n, int r)
{
return factorial(n) / (factorial(r) * factorial(n - r));
}

int main()
{
int n1 = 10;
int r1 = 5;
cout << "n: " << n1 << ", r: " << r1 << endl;
cout << "Value of nCr: " << calculate_nCr(n1, r1) << endl;
int n2 = 3;
int r2 = 2;
cout << "n: " << n2 << ", r: " << r2 << endl;
cout << "Value of nCr: " << calculate_nCr(n2, r2) << endl;
int n3 = 1;
int r3 = 1;
cout << "n: " << n3 << ", r: " << r3 << endl;
cout << "Value of nCr: " << calculate_nCr(n3, r3) << endl;
int n4 = 8;
int r4 = 0;
cout << "n: " << n4 << ", r: " << r4 << endl;
cout << "Value of nCr: " << calculate_nCr(n4, r4) << endl;
int n5 = 4;
int r5 = 4;
cout << "n: " << n5 << ", r: " << r5 << endl;
cout << "Value of nCr: " << calculate_nCr(n5, r5) << endl;
return 0;
}

Sortir:

 n: 10, r: 5
Value of nCr: 252
n: 3, r: 2
Value of nCr: 3
n: 1, r: 1
Value of nCr: 1
n: 8, r: 0
Value of nCr: 1
n: 4, r: 4
Value of nCr: 1

Connexes : Comment imprimer toutes les permutations d'une chaîne donnée en C, C++, JavaScript et Python

Programme Python pour calculer la valeur de nCr

Ensuite, est le code Python pour calculer la valeur de nCr :

 # Python program to calculate the value of nCr
# Function to calculate the factorial of a number
def factorial(num):
if num<=1:
return 1
return num*factorial(num-1)
# Function to calculate the value of nCr
def calculate_nCr(n, r):
return factorial(n) // (factorial(r) * factorial(n - r))
n1 = 10
r1 = 5
print("n:", n1, ",r:", r1)
print("Value of nCr:", calculate_nCr(n1, r1))
n2 = 3
r2 = 2
print("n:", n2, ",r:", r2)
print("Value of nCr:", calculate_nCr(n2, r2))
n3 = 1
r3 = 1
print("n:", n3, ",r:", r3)
print("Value of nCr:", calculate_nCr(n3, r3))
n4 = 8
r4 = 0
print("n:", n4, ",r:", r4)
print("Value of nCr:", calculate_nCr(n4, r4))
n5 = 4
r5 = 4
print("n:", n5, ",r:", r5)
print("Value of nCr:", calculate_nCr(n5, r5))

Sortir:

 n: 10, r: 5
Value of nCr: 252
n: 3, r: 2
Value of nCr: 3
n: 1, r: 1
Value of nCr: 1
n: 8, r: 0
Value of nCr: 1
n: 4, r: 4
Value of nCr: 1

Connexes : Comment trouver la somme d'une série géométrique à l'aide de plusieurs langues

Programme JavaScript pour calculer la valeur de nCr

Voici comment calculer la valeur de nCr à l'aide de JavaScript :

 // JavaScript program to calculate the value of nCr
// Function to calculate the factorial of a number
function factorial(num) {
if (num<=1) {
return 1;
}
return num*factorial(num-1);
}
// Function to calculate the value of nCr
function calculate_nCr(n, r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
var n1 = 10;
var r1 = 5;
document.write("n: " + n1 + ", r:" + r1 + "<br>");
document.write("Value of nCr: " + calculate_nCr(n1, r1) + "<br>");
var n2 = 3;
var r2 = 2;
document.write("n: " + n2 + ", r:" + r2 + "<br>");
document.write("Value of nCr: " + calculate_nCr(n2, r2) + "<br>");
var n3 = 1;
var r3 = 1;
document.write("n: " + n3 + ", r:" + r3 + "<br>");
document.write("Value of nCr: " + calculate_nCr(n3, r3) + "<br>");
var n4 = 8;
var r4 = 0;
document.write("n: " + n4 + ", r:" + r4 + "<br>");
document.write("Value of nCr: " + calculate_nCr(n4, r4) + "<br>");
var n5 = 4;
var r5 = 4;
document.write("n: " + n5 + ", r:" + r5 + "<br>");
document.write("Value of nCr: " + calculate_nCr(n5, r5) + "<br>");

Sortir:

 n: 10, r: 5
Value of nCr: 252
n: 3, r: 2
Value of nCr: 3
n: 1, r: 1
Value of nCr: 1
n: 8, r: 0
Value of nCr: 1
n: 4, r: 4
Value of nCr: 1

Connexes : Comment calculer les intérêts simples et composés

C Programme pour calculer la valeur de nCr

Ci-dessous se trouve le programme C pour calculer la valeur de nCr :

 // C program to calculate the value of nCr
#include <stdio.h>
// Function to calculate the factorial of a number
int factorial(int num)
{
if (num<=1)
{
return 1;
}
return num*factorial(num-1);
}
// Function to calculate the value of nCr
int calculate_nCr(int n, int r)
{
return factorial(n) / (factorial(r) * factorial(n - r));
}

int main()
{
int n1 = 10;
int r1 = 5;
printf("n: %d, r: %d ⁠n", n1, r1);
printf("Value of nCr: %d ⁠n", calculate_nCr(n1, r1));
int n2 = 3;
int r2 = 2;
printf("n: %d, r: %d ⁠n", n2, r2);
printf("Value of nCr: %d ⁠n", calculate_nCr(n2, r2));
int n3 = 1;
int r3 = 1;
printf("n: %d, r: %d ⁠n", n3, r3);
printf("Value of nCr: %d ⁠n", calculate_nCr(n3, r3));
int n4 = 8;
int r4 = 0;
printf("n: %d, r: %d ⁠n", n4, r4);
printf("Value of nCr: %d ⁠n", calculate_nCr(n4, r4));
int n5 = 4;
int r5 = 4;
printf("n: %d, r: %d ⁠n", n5, r5);
printf("Value of nCr: %d ⁠n", calculate_nCr(n5, r5));
return 0;
}

Sortir:

 n: 10, r: 5
Value of nCr: 252
n: 3, r: 2
Value of nCr: 3
n: 1, r: 1
Value of nCr: 1
n: 8, r: 0
Value of nCr: 1
n: 4, r: 4
Value of nCr: 1

Programme Java pour calculer la valeur de nCr

Ci-dessous se trouve le programme Java pour calculer la valeur de nCr :

 // Java program to calculate the value of nCr
public class Main
{
// Function to calculate the factorial of a number
static int factorial(int num) {
if (num <= 1) {
return 1;
}
return num * factorial(num - 1);
}
// Function to calculate the value of nCr
static int calculate_nCr(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
public static void main(String[] args) {
int n1 = 10;
int r1 = 5;
System.out.println("n: " + n1 + ", r: " + r1);
System.out.println("Value of nCr: " + calculate_nCr(n1, r1));
int n2 = 3;
int r2 = 2;
System.out.println("n: " + n2 + ", r: " + r2);
System.out.println("Value of nCr: " + calculate_nCr(n2, r2));
int n3 = 1;
int r3 = 1;
System.out.println("n: " + n3 + ", r: " + r3);
System.out.println("Value of nCr: " + calculate_nCr(n3, r3));
int n4 = 8;
int r4 = 0;
System.out.println("n: " + n4 + ", r: " + r4);
System.out.println("Value of nCr: " + calculate_nCr(n4, r4));
int n5 = 4;
int r5 = 4;
System.out.println("n: " + n5 + ", r: " + r5);
System.out.println("Value of nCr: " + calculate_nCr(n5, r5));
}
}

Sortir:

 n: 10, r: 5
Value of nCr: 252
n: 3, r: 2
Value of nCr: 3
n: 1, r: 1
Value of nCr: 1
n: 8, r: 0
Value of nCr: 1
n: 4, r: 4
Value of nCr: 1

Conclusion

Dans cet article, vous avez appris à calculer la valeur de nCr dans différents langages de programmation. La technique est la même et se transfère bien entre les langues.

Les permutations et les combinaisons sont les principes de comptage utilisés dans diverses situations. Vous pouvez calculer la valeur de nPr en utilisant une approche similaire.