Write a Java program to find the value of combination formula i.e nCr.

combination formula

The above formula is used in mathematics to calculate the combination value, and we will use the same in our java program.

For this, we will write a separate function that will compute and return the factorial of the passed number as an argument.

Using the factorial function we will find the values of n!, (n-r)! and r! and then the result by applying the arithmetic operation as stated by the formula in the above image.


Combination in Java Output

The above method is not so efficient for large numbers because calculating factorial for enormous value of n becomes overhead, especially in competitive programming.

There is one more way to calculate a combination in Java using the second formula as depicted in the 1st Image.

Efficient way to Calculate Combination in Java


Efficiently find combination in Java Output

If you know a better way to find the combination, then please comment below.

Leave a Reply

4 × five =