Combination is a mathematical concept. It refers to the arrangement of several objects where their order is irrelevant. A basic formula describes the number of valid combinations.
In this article, you'll learn how to calculate the value of nCr using Python, C++, JavaScript, C, and Java. Each example also contains sample output for several different values.
How to Calculate nCr
Use the following combination formula to calculate the value of nCr:
nCr = n! / (r! * (n-r)!)
Where:
n = Total number
C = Combination
r = Arrangement
! = Factorial
Problem Statement
You're given the values of n and r. You need to calculate the value of nCr.
Example 1: Let n = 10 and r = 5.
Therefore, nCr = 10! / (5! * (10-5)!) = 10! / (5! * 5!) = 252.
Thus, the output is 252.
Example 2: Let n = 8 and r = 0.
Therefore, nCr = 8! / (0! * (8-0)!) = 8! / (0! * 8!) = 1.
Thus, the output is 1.
C++ Program to Calculate the Value of nCr
Below is the C++ program to calculate the value of 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;
}
Output:
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
Python Program to Calculate the Value of nCr
Next, is the Python code to calculate the Value of 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))
Output:
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
JavaScript Program to Calculate the Value of nCr
Here's how you can calculate the value of nCr using 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>");
Output:
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
C Program to Calculate the Value of nCr
Below is the C program to calculate the value of 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;
}
Output:
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
Java Program to Calculate the Value of nCr
Below is the Java program to calculate the value of 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));
}
}
Output:
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
In this article, you learned how to calculate the value of nCr in different programming languages. The technique is the same, and transfers well between languages.
Permutations and combinations are the principles of counting used in various situations. You can calculate the value of nPr using a similar approach.
0 Comments