Program to print binomial expansion series

Given three integers, A, X and n, the task is to print terms of below binomial expression series.

(A+X)n = nC0AnX0 + nC1An-1X1 + nC2An-2X2 +….+ nCnA0Xn

Examples:

Input : A = 1, X = 1, n = 5
Output : 1 5 10 10 5 1

Input : A = 1, B = 2, n = 6
Output : 1 12 60 160 240 192 64 

Simple Solution : We know that for each value of n there will be (n+1) term in the binomial series. So now we use a simple approach and calculate the value of each element of the series and print it .

nCr = (n!) / ((n-r)! * (r)!)

Below is value of general term. 
Tr+1 = nCn-rAn-rXr
So at each position we have to find the value 
of the general term and print that term .

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print terms of binomial 
// series and also calculate sum of series.
#include <bits/stdc++.h>
using namespace std;
  
// function to calculate factorial of 
// a number
int factorial(int n)
{
    int f = 1;
    for (int i = 2; i <= n; i++)
        f *= i;        
    return f;
}
  
// fuction to print the series
void series(int A, int X, int n)
{     
    // calculating the value of n!
    int nFact = factorial(n);
  
    // loop to display the series
    for (int i = 0; i < n + 1; i++) {
          
        // For calculating the 
        // value of nCr
        int niFact = factorial(n - i);
        int iFact = factorial(i);
  
        // calculating the value of 
        // A to the power k and X to
        // the power k
        int aPow = pow(A, n - i);
        int xPow = pow(X, i);
  
        // display the series
        cout << (nFact * aPow * xPow) /
                 (niFact * iFact) << " ";
    }
}
  
// main fuction started
int main()
{
    int A = 3, X = 4, n = 5;
    series(A, X, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print terms of binomial
// series and also calculate sum of series.
  
import java.io.*;
  
class GFG {
      
    // function to calculate factorial of
    // a number
    static int factorial(int n)
    {
        int f = 1;
        for (int i = 2; i <= n; i++)
            f *= i;
              
        return f;
    }
  
    // fuction to print the series
    static void series(int A, int X, int n)
    {
          
        // calculating the value of n!
        int nFact = factorial(n);
  
        // loop to display the series
        for (int i = 0; i < n + 1; i++) {
  
            // For calculating the
            // value of nCr
            int niFact = factorial(n - i);
            int iFact = factorial(i);
  
            // calculating the value of
            // A to the power k and X to
            // the power k
            int aPow = (int)Math.pow(A, n - i);
            int xPow = (int)Math.pow(X, i);
  
            // display the series
            System.out.print((nFact * aPow * xPow) 
                         / (niFact * iFact) + " ");
        }
    }
  
    // main fuction started
    public static void main(String[] args)
    {
        int A = 3, X = 4, n = 5;
          
        series(A, X, n);
    }
}
  
// This code is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print terms of binomial 
# series and also calculate sum of series.
  
# function to calculate factorial 
# of a number
def factorial(n):
  
    f = 1
    for i in range(2, n+1):
        f *=
          
    return f
  
# Fuction to print the series
def series(A, X, n):
      
    # calculating the value of n!
    nFact = factorial(n)
  
    # loop to display the series
    for i in range(0, n + 1): 
          
        # For calculating the 
        # value of nCr
        niFact = factorial(n - i)
        iFact = factorial(i)
  
        # calculating the value of 
        # A to the power k and X to
        # the power k
        aPow = pow(A, n - i)
        xPow = pow(X, i)
  
        # display the series
        print (int((nFact * aPow * xPow) /
                   (niFact * iFact)), end = " ")
      
# Driver Code
A = 3; X = 4; n = 5
series(A, X, n)
  
# This code is contributed by Smitha Dinesh Semwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print terms of binomial
// series and also calculate sum of series.
using System;
  
class GFG {
      
    // function to calculate factorial of
    // a number
    static int factorial(int n)
    {
        int f = 1;
        for (int i = 2; i <= n; i++)
            f *= i;
              
        return f;
    }
  
    // fuction to print the series
    static void series(int A, int X, int n)
    {
          
        // calculating the value of n!
        int nFact = factorial(n);
  
        // loop to display the series
        for (int i = 0; i < n + 1; i++) {
  
            // For calculating the
            // value of nCr
            int niFact = factorial(n - i);
            int iFact = factorial(i);
  
            // calculating the value of
            // A to the power k and X to
            // the power k
            int aPow = (int)Math.Pow(A, n - i);
            int xPow = (int)Math.Pow(X, i);
  
            // display the series
            Console.Write((nFact * aPow * xPow) 
                     / (niFact * iFact) + " ");
        }
    }
  
    // main fuction started
    public static void Main()
    {
        int A = 3, X = 4, n = 5;
          
        series(A, X, n);
    }
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print 
// terms of binomial 
// series and also 
// calculate sum of series.
  
// function to calculate
// factorial of a number
function factorial($n)
{
    $f = 1;
    for ($i = 2; $i <= $n; $i++)
        $f *= $i
    return $f;
}
  
// fuction to print the series
function series($A, $X, $n)
      
    // calculating the
    // value of n!
    $nFact = factorial($n);
  
    // loop to display 
    // the series
    for ($i = 0; $i < $n + 1; $i++)
    {
          
        // For calculating the 
        // value of nCr
        $niFact = factorial($n - $i);
        $iFact = factorial($i);
  
        // calculating the value of 
        // A to the power k and X to
        // the power k
        $aPow = pow($A, $n - $i);
        $xPow = pow($X, $i);
  
        // display the series
        echo ($nFact * $aPow * $xPow) /
             ($niFact * $iFact) , " ";
    }
}
  
    // Driver Code
    $A = 3;
    $X = 4; 
    $n = 5;
    series($A, $X, $n);
  
// This code is contributed by anuj_67.
?>

chevron_right


Output:

243 1620 4320 5760 3840 1024 

Efficient Solution :
The idea is to compute next term using previous term. We can compute next term in O(1) time. We use below property of Binomial Coefficients.

nCi+1 = nCi*(n-i)/(i+1)

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print terms of binomial
// series and also calculate sum of series.
#include <bits/stdc++.h>
using namespace std;
  
// fuction to print the series
void series(int A, int X, int n)
{
    // Calculating and printing first term
    int term = pow(A, n);
    cout << term << " ";
  
    // Computing and printing remaining terms
    for (int i = 1; i <= n; i++) {
  
        // Find current term using previous terms
        // We increment power of X by 1, decrement
        // power of A by 1 and compute nCi using 
        // previous term by multiplying previous
        // term with (n - i + 1)/i
        term = term * X * (n - i + 1)/(i * A);
  
        cout << term << " ";
    }
}
  
// main fuction started
int main()
{
    int A = 3, X = 4, n = 5;
    series(A, X, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print terms of binomial
// series and also calculate sum of series.
  
import java.io.*;
  
class GFG {
      
    // fuction to print the series
    static void series(int A, int X, int n)
    {
          
        // Calculating and printing first 
        // term
        int term = (int)Math.pow(A, n);
        System.out.print(term + " ");
  
        // Computing and printing 
        // remaining terms
        for (int i = 1; i <= n; i++) {
  
            // Find current term using 
            // previous terms We increment
            // power of X by 1, decrement
            // power of A by 1 and compute 
            // nCi using previous term by 
            // multiplying previous term 
            // with (n - i + 1)/i
            term = term * X * (n - i + 1
                                / (i * A);
  
            System.out.print(term + " ");
        }
    }
  
    // main fuction started
    public static void main(String[] args)
    {
        int A = 3, X = 4, n = 5;
          
        series(A, X, n);
    }
}
  
// This code is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to print terms of binomial
# series and also calculate sum of series.
  
# Fuction to print the series
def series(A, X, n):
  
    # Calculating and printing first term
    term = pow(A, n)
    print(term, end = " ")
  
    # Computing and printing remaining terms
    for i in range(1, n+1): 
  
        # Find current term using previous terms
        # We increment power of X by 1, decrement
        # power of A by 1 and compute nCi using 
        # previous term by multiplying previous
        # term with (n - i + 1)/i
        term = int(term * X * (n - i + 1)/(i * A))
  
        print(term, end = " ")
      
# Driver Code
A = 3; X = 4; n = 5
series(A, X, n)
  
# This code is contributed by Smitha Dinesh Semwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print terms of binomial
// series and also calculate sum of series.
  
using System;
  
public class GFG {
      
    // fuction to print the series
    static void series(int A, int X, int n)
    {
          
        // Calculating and printing first 
        // term
        int term = (int)Math.Pow(A, n);
        Console.Write(term + " ");
  
        // Computing and printing 
        // remaining terms
        for (int i = 1; i <= n; i++) {
  
            // Find current term using 
            // previous terms We increment
            // power of X by 1, decrement
            // power of A by 1 and compute 
            // nCi using previous term by 
            // multiplying previous term 
            // with (n - i + 1)/i
            term = term * X * (n - i + 1) 
                                / (i * A);
  
          Console.Write(term + " ");
        }
    }
  
    // main fuction started
    public static void Main()
    {
        int A = 3, X = 4, n = 5;
          
        series(A, X, n);
    }
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print
// terms of binomial
// series and also 
// calculate sum of
// series.
  
// fuction to print
// the series
function series($A, $X, $n)
{
      
    // Calculating and printing
    // first term
    $term = pow($A, $n);
    echo $term , " ";
  
    // Computing and printing
    // remaining terms
    for ($i = 1; $i <= $n; $i++)
    {
  
        // Find current term 
        // using previous terms
        // We increment power 
        // of X by 1, decrement
        // power of A by 1 and 
        // compute nCi using 
        // previous term by 
        // multiplying previous
        // term with (n - i + 1)/i
        $term = $term * $X * ($n - $i + 1) / 
                                 ($i * $A);
  
        echo $term , " ";
    }
}
  
    // Driver Code
    $A = 3;
    $X = 4; 
    $n = 5;
    series($A, $X, $n);
  
// This code is contributed by anuj_67.
?>

chevron_right


Output:

243 1620 4320 5760 3840 1024 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m