# How to Calculate the Value of nCr

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 numberC = Combinationr = 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 using namespace std;// Function to calculate the factorial of a numberint factorial(int num){    if (num<=1)    {        return 1;    }    return num*factorial(num-1);}// Function to calculate the value of nCrint 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: 5Value of nCr: 252n: 3, r: 2Value of nCr: 3n: 1, r: 1Value of nCr: 1n: 8, r: 0Value of nCr: 1n: 4, r: 4Value 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 numberdef factorial(num):    if num<=1:        return 1    return num*factorial(num-1)# Function to calculate the value of nCrdef calculate_nCr(n, r):    return factorial(n) // (factorial(r) * factorial(n - r))n1 = 10r1 = 5print("n:", n1, ",r:", r1)print("Value of nCr:", calculate_nCr(n1, r1))n2 = 3r2 = 2print("n:", n2, ",r:", r2)print("Value of nCr:", calculate_nCr(n2, r2))n3 = 1r3 = 1print("n:", n3, ",r:", r3)print("Value of nCr:", calculate_nCr(n3, r3))n4 = 8r4 = 0print("n:", n4, ",r:", r4)print("Value of nCr:", calculate_nCr(n4, r4))n5 = 4r5 = 4print("n:", n5, ",r:", r5)print("Value of nCr:", calculate_nCr(n5, r5))``

Output:

``n: 10, r: 5Value of nCr: 252n: 3, r: 2Value of nCr: 3n: 1, r: 1Value of nCr: 1n: 8, r: 0Value of nCr: 1n: 4, r: 4Value 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 numberfunction factorial(num) {    if (num<=1) {        return 1;    }    return num*factorial(num-1);}// Function to calculate the value of nCrfunction calculate_nCr(n, r) {    return factorial(n) / (factorial(r) * factorial(n - r));}var n1 = 10;var r1 = 5;document.write("n: " + n1 + ", r:" + r1 + "");document.write("Value of nCr: " + calculate_nCr(n1, r1) + "");var n2 = 3;var r2 = 2;document.write("n: " + n2 + ", r:" + r2 + "");document.write("Value of nCr: " + calculate_nCr(n2, r2) + "");var n3 = 1;var r3 = 1;document.write("n: " + n3 + ", r:" + r3 + "");document.write("Value of nCr: " + calculate_nCr(n3, r3) + "");var n4 = 8;var r4 = 0;document.write("n: " + n4 + ", r:" + r4 + "");document.write("Value of nCr: " + calculate_nCr(n4, r4) + "");var n5 = 4;var r5 = 4;document.write("n: " + n5 + ", r:" + r5 + "");document.write("Value of nCr: " + calculate_nCr(n5, r5) + "");``

Output:

``n: 10, r: 5Value of nCr: 252n: 3, r: 2Value of nCr: 3n: 1, r: 1Value of nCr: 1n: 8, r: 0Value of nCr: 1n: 4, r: 4Value 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 // Function to calculate the factorial of a numberint factorial(int num){    if (num<=1)    {        return 1;    }    return num*factorial(num-1);}// Function to calculate the value of nCrint 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: 5Value of nCr: 252n: 3, r: 2Value of nCr: 3n: 1, r: 1Value of nCr: 1n: 8, r: 0Value of nCr: 1n: 4, r: 4Value 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 nCrpublic 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: 5Value of nCr: 252n: 3, r: 2Value of nCr: 3n: 1, r: 1Value of nCr: 1n: 8, r: 0Value of nCr: 1n: 4, r: 4Value 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.

Author: Yuvraj Chandra

Source: Yuvraj Chandra.” How to Calculate the Value of nCr”. Retrieved From https://www.makeuseof.com/calculate-the-value-of-ncr-combination/