Java Program To Perform nCr
In this tutorial, we will learn how to find the value of nCr. The nCr value gives the number of ways, disregarding order, that r objects can be chosen from among n objects; more formally, the number of r-element subsets (or r-combinations) of an n-element set. But before moving forward if you are not familiar with the concept of loops in java, then do check the article on Loops in Java.
The below formula is used to calculate the nCr value.
nCr = (n!)/((n-r)! * r!)
Input: Enter the value of n: 5
Enter the value of r: 2
Output: 5C2 = 10
Two cases arise for the above problem:
Case 1: When the values are user-defined
Case 2: When the values are pre-defined
Let us look at each of these cases separately.
Program 1: To find the nCr value in Java
In this program, we will find the nCr value when the values are user-defined. This means, first we will ask the user to enter the n and r values and then we will calculate the nCr value using the formula. Here, we will use a for loop to calculate the factorial.
Algorithm:
-
Start
-
Declare the variables.
-
Ask the user to initialize the variables.
-
Check whether it is possible to find the nCr value or not.
-
If possible, then call a method to calculate the nCr.
-
Calculate the factorial of a number using a for loop.
-
Find the nCr value using the formula.
-
Prit the nCr value.
-
If it is not possible to calculate the nCr value, then enter the value of n and r such that n>=r.
-
Stop
The below example illustrates the implementation of the above algorithm.
//Java Program to find the nCr
import java.util.*;
public class Main
{
//Method to calculate the nCr value
static int nCr(int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
//Method to calculate the factorial of the number
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static void main(String[] args)
{
//Take input from the variables
//Create instance of the Scanner Class
Scanner sc = new Scanner(System.in);
int n,r; //Declare variables
System.out.println("Enter the value of n :");
n = sc.nextInt(); //Initialize the variables
System.out.println("Enter the value of r :");
r = sc.nextInt(); //Initialize the variables
if(n>=r)
{
//Print the nCr value
System.out.println("Value of "+ n+"C"+r+"= "+nCr(n, r));
}
else
System.out.println("n value should be greater than or equals to r value");
}
}
Enter the value of n: 5
Enter the value of r: 2
Value of 5C2= 10
Program 2: To Find the nCr value in Java
In this program, we will find the nCr value when the values are pre-defined in the program.
Algorithm:
-
Start
-
Declare and initialize the variables.
-
Check whether it is possible to find the nCr value or not.
-
If possible, then call a method to calculate the nCr.
-
Calculate the factorial of a number.
-
Find the nCr value using the formula.
-
Prit the nCr value.
-
If it is not possible to calculate the nCr value, then enter the value of n and r such that n>=r.
-
Stop
The below example illustrates the implementation of the above algorithm.
//Java Program to find the nCr
public class Main
{
//Method to calculate the nCr value
static int nCr(int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
//Method to calculate the factorial of the number
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static void main(String[] args)
{
int n=7,r=2; //Declare and initialize the variables
System.out.println("The entered value of n is :"+ n);
System.out.println("The entered value of r is :"+ r);
if(n>=r)
{
//Print the nCr value
System.out.println("Value of "+ n+"C"+r+"= "+nCr(n, r));
}
else
System.out.println("n value should be greater than or equals to r value");
}
}
The entered value of n is:7
The entered value of r is:2
Value of 7C2= 21
Program 3: To Find the nCr value in Java
In this program, we will find the nCr value when the values are user-defined. This means, first we will ask the user to enter the n and r values and then we will calculate the nCr value using the formula. Here, we will use a while loop to calculate the factorial.
Algorithm:
-
Start
-
Declare the variables.
-
Ask the user to initialize the variables.
-
Check whether it is possible to find the nCr value or not.
-
If possible, then call a method to calculate the nCr.
-
Calculate the factorial of a number using a while loop.
-
Find the nCr value using the formula.
-
Print the nCr value.
-
If it is not possible to calculate the nCr value, then enter the value of n and r such that n>=r.
-
Stop
The below example illustrates the implementation of the above algorithm.
//Java Program to find nCr
import java.util.*;
public class Main
{
//Calculate factorial of the number
static double fact(double n)
{
int i=1;
double fact=1;
while(i<=n)
{
fact=fact*i;
i++;
}
return fact;
}
//Calculate the combination value
static double combination(int n,int r)
{
double com=fact(n)/(fact(n-r)*fact(r));
return com;
}
//Driver Code
public static void main(String arg[])
{
//Take input from the user
//Create an instance of the Scanner class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of n : ");
int n=sc.nextInt();
System.out.println("Enter the value of r : ");
int r=sc.nextInt();
//Check whether it is possible to find the nCr value.
if(n>=r)
{
System.out.println("The value of "+n+"c"+r+" is : "
+combination(n,r));
}
else
System.out.println("n value should be greater than or equals to r value");
}
}
Enter the value of n: 8
Enter the value of r: 3
The value of 8c3 is: 56.0