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:

Where:

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:

Output:

Python Program to Calculate the Value of nCr

Next, is the Python code to calculate the Value of nCr:

Output:

JavaScript Program to Calculate the Value of nCr

Here’s how you can calculate the value of nCr using JavaScript:

Output:

C Program to Calculate the Value of nCr

Below is the C program to calculate the value of nCr:

Output:

Java Program to Calculate the Value of nCr

Below is the Java program to calculate the value of nCr:

Output:

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.