Open In App

Middle term in the binomial expansion series

Last Updated : 07 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given three integers A, X, and n. The task is to find the middle term in the binomial expansion series. 

Examples: 

Input : A = 1, X = 1, n = 6
Output : MiddleTerm = 20

Input : A = 2, X = 4, n = 7
Output : MiddleTerm1 = 35840, MiddleTerm2 = 71680

Approach 
 

(A + X)n = nC0 An X0 + nC1 An-1 X1 + nC2 An-2 X2 + ……… + nCn-1 A1 Xn-1 + nCn A0 Xn 
Total number of term in the binomial expansion of (A + X)n is (n + 1).
General term in binomial expansion is given by: 
Tr+1 = nCr An-r Xr
If n is even number: 
Let m be the middle term of binomial expansion series, then 
n = 2m 
m = n / 2 
We know that there will be n + 1 term so, 
n + 1 = 2m +1 
In this case, there will is only one middle term. This middle term is (m + 1)th term. 
Hence, the middle term 
Tm+1 = nCmAn-mXm
if n is odd number: 
Let m be the middle term of binomial expansion series, then 
let n = 2m + 1 
m = (n-1) / 2 
number of terms = n + 1 = 2m + 1 + 1 = 2m + 2 
In this case there will be two middle terms. These middle terms will be (m + 1)th and (m + 2)th term. 
Hence, the middle terms are : 
Tm+1 = nC(n-1)/2 A(n+1)/2 X(n-1)/2 
Tm+2 = nC(n+1)/2 A(n-1)/2 X(n+1)/2 
 

 

 

C++




// C++ program to find the middle term
// in binomial expansion series.
#include <bits/stdc++.h>
using namespace std;
 
// function to calculate
// factorial of a number
int factorial(int n)
    int fact = 1;
    for (int i = 1; i <= n; i++)
        fact *= i;
        
    return fact;
}
 
// Function to find middle term in
// binomial expansion series.
void findMiddleTerm(int A, int X, int n)
{
    int i, j, aPow, xPow;
    float middleTerm1, middleTerm2;
 
    if (n % 2 == 0)
    {
        // If n is even
         
        // calculating the middle term
        i = n / 2;
 
        // calculating the value of A to
        // the power k and X to the power k
        aPow = (int)pow(A, n - i);
        xPow = (int)pow(X, i);
 
        middleTerm1 = ((float)factorial(n) /
          (factorial(n - i) * factorial(i)))
                              * aPow * xPow;
                               
        cout << "MiddleTerm = "
             << middleTerm1 << endl;
    }
    else {
 
        // If n is odd
         
        // calculating the middle term
        i = (n - 1) / 2;
        j = (n + 1) / 2;
 
        // calculating the value of A to the
        // power k and X to the power k
        aPow = (int)pow(A, n - i);
        xPow = (int)pow(X, i);
 
        middleTerm1 = ((float)factorial(n) /
           (factorial(n - i) * factorial(i)))
                               * aPow * xPow;
 
        // calculating the value of A to the
        // power k and X to the power k
        aPow = (int)pow(A, n - j);
        xPow = (int)pow(X, j);
 
        middleTerm2 = ((float)factorial(n) /
           (factorial(n - j) * factorial(j)))
                               * aPow * xPow;
 
        cout << "MiddleTerm1 = "
                      << middleTerm1 << endl;
                       
        cout << "MiddleTerm2 = "
                      << middleTerm2 << endl;
    }
}
 
// Driver code
int main()
{
    int n = 5, A = 2, X = 3;
     
    // function call
    findMiddleTerm(A, X, n);
 
    return 0;
}


Java




// Java program to find the middle term
// in binomial expansion series.
import java.math.*;
 
class GFG {
 
    // function to calculate factorial
    // of a number
    static int factorial(int n)
    {
        int fact = 1, i;
        if (n == 0)
            return 1;
        for (i = 1; i <= n; i++)
            fact *= i;
             
        return fact;
    }
     
    // Function to find middle term in
    // binomial expansion series.
    static void findmiddle(int A, int X, int n)
    {
        int i, j, aPow, xPow;
        float middleTerm1, middleTerm2;
 
        if (n % 2 == 0)
        {
            // If n is even
             
            // calculating the middle term
            i = n / 2;
 
            // calculating the value of A to
            // the power k and X to the power k
            aPow = (int)Math.pow(A, n - i);
            xPow = (int)Math.pow(X, i);
     
            middleTerm1 = ((float)factorial(n) /
              (factorial(n - i) * factorial(i)))
                                  * aPow * xPow;
            System.out.println("MiddleTerm = "
                                 + middleTerm1);
        }
        else {
             
            // If n is odd
 
            // calculating the middle term
            i = (n - 1) / 2;
            j = (n + 1) / 2;
 
            // calculating the value of A to the
            // power k and X to the power k
            aPow = (int)Math.pow(A, n - i);
            xPow = (int)Math.pow(X, i);
     
            middleTerm1 = ((float)factorial(n) /
               (factorial(n - i) * factorial(i)))
                                   * aPow * xPow;
     
            // calculating the value of A to the
            // power k and X to the power k
            aPow = (int)Math.pow(A, n - j);
            xPow = (int)Math.pow(X, j);
     
            middleTerm2 = ((float)factorial(n) /
               (factorial(n - j) * factorial(j)))
                                   * aPow * xPow;
 
            System.out.println("MiddleTerm1 = "
                                  + middleTerm1);
                   
            System.out.println("MiddleTerm2 = "
                                  + middleTerm2);
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 6, A = 2, X = 4;
 
        // calling the function
        findmiddle(A, X, n);
    }
}


Python3




# Python3 program to find the middle term
# in binomial expansion series.
import math
 
# function to calculate
# factorial of a number
def factorial(n) :
     
    fact = 1
    for i in range(1, n+1) :
        fact = fact * i
 
    return fact;
 
# Function to find middle term in
# binomial expansion series.
def findMiddleTerm(A, X, n) :
 
    if (n % 2 == 0) :
         
        # If n is even
         
        # calculating the middle term
        i = int(n / 2)
 
        # calculating the value of A to
        # the power k and X to the power k
        aPow = int(math.pow(A, n - i))
        xPow = int(math.pow(X, i))
 
        middleTerm1 = ((math.factorial(n) /
                       (math.factorial(n - i)
                       * math.factorial(i)))
                       * aPow * xPow)
                                 
        print ("MiddleTerm = {}" .
                     format(middleTerm1))
 
    else :
 
        # If n is odd
         
        # calculating the middle term
        i = int((n - 1) / 2)
        j = int((n + 1) / 2)
 
        # calculating the value of A to the
        # power k and X to the power k
        aPow = int(math.pow(A, n - i))
        xPow = int(math.pow(X, i))
 
        middleTerm1 = ((math.factorial(n)
                    / (math.factorial(n - i)
                    * math.factorial(i)))
                        * aPow * xPow)
 
        # calculating the value of A to the
        # power k and X to the power k
        aPow = int(math.pow(A, n - j))
        xPow = int(math.pow(X, j))
 
        middleTerm2 = ((math.factorial(n)
                   / (math.factorial(n - j)
                   * math.factorial(j)))
                      * aPow * xPow)
 
        print ("MiddleTerm1 = {}" .
               format(int(middleTerm1)))
                         
        print ("MiddleTerm2 = {}" .
               format(int(middleTerm2)))
 
# Driver code
n = 5
A = 2
X = 3
 
# function call
findMiddleTerm(A, X, n)
 
# This code is contributed by
# manishshaw1.


C#




// C# program to find the middle term
// in binomial expansion series.
using System;
 
class GFG {
 
    // function to calculate factorial
    // of a number
    static int factorial(int n)
    {
        int fact = 1, i;
        if (n == 0)
            return 1;
        for (i = 1; i <= n; i++)
            fact *= i;
             
        return fact;
    }
     
    // Function to find middle term in
    // binomial expansion series.
    static void findmiddle(int A, int X, int n)
    {
        int i, j, aPow, xPow;
        float middleTerm1, middleTerm2;
 
        if (n % 2 == 0)
        {
            // If n is even
             
            // calculating the middle term
            i = n / 2;
 
            // calculating the value of A to
            // the power k and X to the power k
            aPow = (int)Math.Pow(A, n - i);
            xPow = (int)Math.Pow(X, i);
     
            middleTerm1 = ((float)factorial(n) /
            (factorial(n - i) * factorial(i)))
                                * aPow * xPow;
            Console.WriteLine("MiddleTerm = "
                                + middleTerm1);
        }
        else {
             
            // If n is odd
 
            // calculating the middle term
            i = (n - 1) / 2;
            j = (n + 1) / 2;
 
            // calculating the value of A to the
            // power k and X to the power k
            aPow = (int)Math.Pow(A, n - i);
            xPow = (int)Math.Pow(X, i);
     
            middleTerm1 = ((float)factorial(n) /
            (factorial(n - i) * factorial(i)))
                                * aPow * xPow;
     
            // calculating the value of A to the
            // power k and X to the power k
            aPow = (int)Math.Pow(A, n - j);
            xPow = (int)Math.Pow(X, j);
     
            middleTerm2 = ((float)factorial(n) /
            (factorial(n - j) * factorial(j)))
                                * aPow * xPow;
 
            Console.WriteLine("MiddleTerm1 = "
                                + middleTerm1);
                 
            Console.WriteLine("MiddleTerm2 = "
                                + middleTerm2);
        }
    }
 
    // Driver code
    public static void Main()
    {
        int n = 5, A = 2, X = 3;
 
        // calling the function
        findmiddle(A, X, n);
    }
}
 
// This code is contributed by anuj_67.


PHP




<?php
// PHP program to find the middle term
// in binomial expansion series.
 
// function to calculate
// factorial of a number
function factorial(int $n)
{
    $fact = 1;
    for($i = 1; $i <= $n; $i++)
        $fact *= $i;
         
    return $fact;
}
 
// Function to find middle term in
// binomial expansion series.
function findMiddleTerm($A, $X, $n)
{
    $i; $j;
    $aPow; $xPow;
    $middleTerm1;
    $middleTerm2;
 
    if ($n % 2 == 0)
    {
         
        // If n is even
        // calculating the middle term
        $i = $n / 2;
 
        // calculating the value of A to
        // the power k and X to the power k
        $aPow = pow($A, $n - i);
        $xPow = pow($X, $i);
 
        $middleTerm1 = $factorial($n) /
        (factorial($n - $i) * factorial($i))
                            * $aPow * $xPow;
                             
        echo "MiddleTerm = ","\n",
            $middleTerm1 ;
    }
    else
    {
 
        // If n is odd
         
        // calculating the middle term
        $i = ($n - 1) / 2;
        $j = ($n + 1) / 2;
 
        // calculating the value of A to the
        // power k and X to the power k
        $aPow = pow($A, $n - $i);
        $xPow = pow($X, $i);
 
        $middleTerm1 = ((float)factorial($n) /
        (factorial($n - $i) * factorial($i)))
                            * $aPow * $xPow;
 
        // calculating the value of A to the
        // power k and X to the power k
        $aPow = pow($A, $n - $j);
        $xPow = pow($X, $j);
 
        $middleTerm2 = factorial($n) /
        (factorial($n - $j) * factorial($j))
                            * $aPow * $xPow;
 
        echo "MiddleTerm1 = "
                    , $middleTerm1,"\n" ;
                     
        echo "MiddleTerm2 = "
                    , $middleTerm2 ;
    }
}
 
    // Driver code
    $n = 5;
    $A = 2;
    $X = 3;
     
    // function call
    findMiddleTerm($A, $X, $n);
 
// This code is contributed by Vishal Tripathi.
?>


Javascript




<script>
 
// JavaScript program to find the middle term
// in binomial expansion series.
 
// function to calculate
// factorial of a number
function factorial(n)
{
    let fact = 1;
    for (let i = 1; i <= n; i++)
        fact *= i;
         
    return fact;
}
 
// Function to find middle term in
// binomial expansion series.
function findMiddleTerm(A, X, n)
{
    let i, j, aPow, xPow;
    let middleTerm1, middleTerm2;
 
    if (n % 2 == 0)
    {
        // If n is even
         
        // calculating the middle term
        i = Math.floor(n / 2);
 
        // calculating the value of A to
        // the power k and X to the power k
        aPow = Math.floor(Math.pow(A, n - i));
        xPow = Math.floor(Math.pow(X, i));
 
        middleTerm1 = Math.floor(factorial(n) /
        (factorial(n - i) * factorial(i)))
                            * aPow * xPow;
                                 
        document.write("MiddleTerm = "
            + middleTerm1 + "<br>");
    }
    else {
 
        // If n is odd
         
        // calculating the middle term
        i = Math.floor((n - 1) / 2);
        j = Math.floor((n + 1) / 2);
 
        // calculating the value of A to the
        // power k and X to the power k
        aPow = Math.floor(Math.pow(A, n - i));
        xPow = Math.floor(Math.pow(X, i));
 
        middleTerm1 = Math.floor(factorial(n) /
        (factorial(n - i) * factorial(i)))
                            * aPow * xPow;
 
        // calculating the value of A to the
        // power k and X to the power k
        aPow = Math.floor(Math.pow(A, n - j));
        xPow = Math.floor(Math.pow(X, j));
 
        middleTerm2 = Math.floor(factorial(n) /
        (factorial(n - j) * factorial(j)))
                            * aPow * xPow;
 
        document.write("MiddleTerm1 = "
                    + middleTerm1 + "<br>");
                         
        document.write("MiddleTerm2 = "
                    + middleTerm2 + "<br>");
    }
}
 
// Driver code
 
    let n = 5, A = 2, X = 3;
     
    // function call
    findMiddleTerm(A, X, n);
 
// This code is contributed by Surbhi Tyagi.
 
</script>


Output: 

MiddleTerm1 = 720
MiddleTerm2 = 1080

 

Time Complexity: O(n)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads