Pascal triangle recursion java – Java Program to Find Binomial Coefficient Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Add Two Numbers by Using Recursion

In this program we are going to see how to find binomial coefficient by using recursion in Java programming language.

Java Program to Find Binomial Coefficient Using Recursion

Explanation:

Pascal triangle recursion java: Binomial coefficient are the coefficient that occurs in the binomial theorem.

Binomial coefficient can be given by the formula = (n, k) = n!/k!(n-k)! Where n >= k >= 0

Binomial coefficient can also be rewritten as (n, k) = (n-1,k) + (n-1,k-1)

Lets assume n = 6, k = 4

Binomial coefficient of (6,4) = 6!/(4!(6-4)!) = 6!/(4!*2!) = (6*5)/2 = 15

Now let’s see different ways to find binomial coefficient by using Recursion.

Method-1: Java Program to Find Binomial Coefficient By Using Static Input and Recursion

Approach:

  • Declare and initialize an integer variable ‘n
  • Declare and initialize an integer variable ‘k
  • Call a user defined method binomialCoefficient() and pass the ‘n’, ‘k’ as parameter.
  • Inside the user defined method we will check the binomial coefficient boundary condition: if n==k or k==0 then it returns 1 to the main() method else it calls the same binomialCoefficient() method recursively. “binomialCoefficient(n-1,k-1) + binomialCoefficient(n-1,k)
  • Now the value of the user defined method binomialCoefficient() is stored in an integer variable say ‘b’.
  • Print the value of binomial coefficient.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        //declare and initialize an integer variable n
        int n = 6;
        //declare and initialize an integer variable k
        int k = 4;
        //define the method and store the value inside an integer variable say ‘b’
        int b = binomialCoefficient(n,k);
        //print the result
        System.out.println("The binomial coefficient of ("+n+", "+k+") is: "+b);
    }
    
    //binomialCoefficient() method
    static int binomialCoefficient(int n, int k)
    {
        //checking the binomial boundary condition
        if(k==0 || k==n)
            return 1;
        //method to return binomialCoefficient using recursion
        return binomialCoefficient(n-1,k-1) + binomialCoefficient(n-1,k);
    }
}
Output:

The binomial coefficient of (6, 4) is: 15

Method-2: Java Program to Find Binomial Coefficient By Using User Input and Recursion

Approach:

  • Create a scanner class.
  • Declare two integer variables say ‘n’, ‘k
  • Prompt the user to enter the values for n, k respectively.
  • Call a user defined method binomialCoefficient() and pass the ‘n’, ‘k’ as parameter.
  • Inside the user defined method we will check the binomial coefficient boundary condition: if n==k or k==0 then it returns 1 to the main() method else it calls the same binomialCoefficient() method recursively. “binomialCoefficient(n-1,k-1) + binomialCoefficient(n-1,k)
  • Now the value of the user defined method binomialCoefficient() is stored in an integer variable say ‘b’.
  • Print the value of binomial coefficient.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // create a scanner class
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the value of n:");
        //declare an integer variable ‘n’and initialize it by user input using scanner class.
        int n = s.nextInt();
        System.out.println("Enter the value of k:");
        //declare an integer variable ‘k’and initialize it by user input using scanner class.
        int k = s.nextInt();
        //define the method and store the value inside an integer variable say ‘b’
        int b = binomialCoefficient(n,k);
        //print the result
        System.out.println("The binomial coefficient of ("+n+", "+k+") is: "+b);
    }
    
    //binomialCoefficient() method
    static int binomialCoefficient(int n, int k)
    {
        //checking the binomial boundary condition
        if(k==0 || k==n)
            return 1;
        //method to return binomialCoefficient using recursion
        return binomialCoefficient(n-1,k-1) + binomialCoefficient(n-1,k);
    }
}
Output:

Enter the value of n:
20
Enter the value of k:
18
The binomial coefficient of (20, 18) is: 190

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Related Java Programs: