In the previous article, we have seen Java Program to Calculate Future Investment Value

In this article we will see how to calculate nCr using Java programming language.

## Java Program to Calculate nCr

Before jumping into the program let’s know about nCr.

**nCr:**

nCr is the probability function which represents combinations.

nCr is the selection of objects from group of objects without caring about order of objects means in case of nCr order of objects does not matter.

nCr = n!/[r! (n-r)!]

Where, selecting `r`

objects from `n`

number of objects.

Let’s see different ways to find future investment value.

### Method-1: Java Program to Calculate nCr By Using Static Input Value

**Approach:**

- Declare the value of
`n`

and`r`

value. - Check if
`n`

value is greater than`r`

value or not if yes then call`findCombination()`

method. - Else nCr value can not be found out as
`n`

value is less than`r`

. - Declare a method
`factorial()`

to find factorial of a number. - Inside
`findCombination()`

method using the formula find nCr value.

**Program:**

import java.util.*; class Main { public static void main(String arg[]) { //Scanner class object created Scanner sc=new Scanner(System.in); //n value declared int n=20; //r value declared int r=10; //checking n is greater than equal to r or not if(n>=r) { System.out.println("Value of "+n+"c"+r+" : "+ findCombination(n,r)); } else { System.out.println("r value can not be more than n value"); } } //factorial() method to find factorial of a number public static double factorial(double n) { int i=1; double fact=1; while(i<=n) { fact=fact*i; i++; } return fact; } //findCombination() method to find combinations i.e. nCr value public static double findCombination(int n, int r) { //finding nCr value double nCr=factorial(n)/(factorial(r)*(factorial(n-r))); //returning nCr value return nCr; } }

Output: Value of 20c10 : 184756.0

### Method-2: Java Program to Calculate nCr By Using User Input Value

**Approach:**

- Take input of
`n`

and`r`

value. - Check if
`n`

value is greater than`r`

value or not if yes then call`findCombination()`

method. - Else nCr value can not be found out as
`n`

value is less than`r`

. - Declare a method
`factorial()`

to find factorial of a number. - Inside
`findCombination()`

method using the formula find nCr value.

**Program:**

import java.util.*; class Main { public static void main(String arg[]) { //Scanner class object created Scanner sc=new Scanner(System.in); //taking input of n value System.out.println("Enter n value: "); int n=sc.nextInt(); //taking input of r value System.out.println("Enter r value: "); int r=sc.nextInt(); //checking n is greater than equal to r or not if(n>=r) { System.out.println("Value of "+n+"c"+r+" : "+ findCombination(n,r)); } else { System.out.println("r value can not be more than n value"); } } //factorial() method to find factorial of a number public static double factorial(double n) { int i=1; double fact=1; while(i<=n) { fact=fact*i; i++; } return fact; } //findCombination() method to find combinations i.e. nCr value public static double findCombination(int n, int r) { //finding nCr value double nCr=factorial(n)/(factorial(r)*(factorial(n-r))); //returning nCr value return nCr; } }

Output: Enter n value: 10 Enter r value: 5 Value of 10c5 : 252.0

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

**Related Java Programs:**