Signup/Sign In

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:

  1. Start

  2. Declare the variables.

  3. Ask the user to initialize the variables.

  4. Check whether it is possible to find the nCr value or not.

  5. If possible, then call a method to calculate the nCr.

  6. Calculate the factorial of a number using a for loop.

  7. Find the nCr value using the formula.

  8. Prit the nCr value.

  9. If it is not possible to calculate the nCr value, then enter the value of n and r such that n>=r.

  10. 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:

  1. Start

  2. Declare and initialize the variables.

  3. Check whether it is possible to find the nCr value or not.

  4. If possible, then call a method to calculate the nCr.

  5. Calculate the factorial of a number.

  6. Find the nCr value using the formula.

  7. Prit the nCr value.

  8. If it is not possible to calculate the nCr value, then enter the value of n and r such that n>=r.

  9. 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:

  1. Start

  2. Declare the variables.

  3. Ask the user to initialize the variables.

  4. Check whether it is possible to find the nCr value or not.

  5. If possible, then call a method to calculate the nCr.

  6. Calculate the factorial of a number using a while loop.

  7. Find the nCr value using the formula.

  8. Print the nCr value.

  9. If it is not possible to calculate the nCr value, then enter the value of n and r such that n>=r.

  10. 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



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.