Program to print binomial expansion series
Last Updated :
21 Jun, 2022
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++
// 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;
}
// function 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 function started
int main()
{
int A = 3, X = 4, n = 5;
series(A, X, n);
return 0;
}
Java
// 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;
}
// function 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 function 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.
Python3
# 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 *= i
return f
# Function 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.
C#
// 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;
}
// function 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 function started
public static void Main()
{
int A = 3, X = 4, n = 5;
series(A, X, n);
}
}
// This code is contributed by anuj_67.
PHP
<?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;
}
// function 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.
?>
JavaScript
<script>
// JavaScript program to print terms of binomial
// series and also calculate sum of series.
// function to calculate factorial of
// a number
function factorial(n)
{
let f = 1;
for (let i = 2; i <= n; i++)
f *= i;
return f;
}
// function to print the series
function series(A, X, n)
{
// calculating the value of n!
let nFact = factorial(n);
// loop to display the series
for (let i = 0; i < n + 1; i++) {
// For calculating the
// value of nCr
let niFact = factorial(n - i);
let iFact = factorial(i);
// calculating the value of
// A to the power k and X to
// the power k
let aPow = Math.pow(A, n - i);
let xPow = Math.pow(X, i);
// display the series
document.write((nFact * aPow * xPow)
/ (niFact * iFact) + " ");
}
}
// Driver Code
let A = 3, X = 4, n = 5;
series(A, X, n);
// This code is contributed by chinmoy1997pal.
</script>
Output: 243 1620 4320 5760 3840 1024
Time complexity : O(n2)
Auxiliary Space : O(1)
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++
// CPP program to print terms of binomial
// series and also calculate sum of series.
#include <bits/stdc++.h>
using namespace std;
// function 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 function started
int main()
{
int A = 3, X = 4, n = 5;
series(A, X, n);
return 0;
}
Java
// Java program to print terms of binomial
// series and also calculate sum of series.
import java.io.*;
class GFG {
// function 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 function 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.
Python3
# Python 3 program to print terms of binomial
# series and also calculate sum of series.
# Function 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.
C#
// C# program to print terms of binomial
// series and also calculate sum of series.
using System;
public class GFG {
// function 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 function started
public static void Main()
{
int A = 3, X = 4, n = 5;
series(A, X, n);
}
}
// This code is contributed by anuj_67.
PHP
<?php
// PHP program to print
// terms of binomial
// series and also
// calculate sum of
// series.
// function 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.
?>
JavaScript
<script>
// JavaScript program to print terms of binomial
// series and also calculate sum of series.
// function to print the series
function series(A, X, n)
{
// Calculating and printing first term
let term = Math.pow(A, n);
document.write(term + " ");
// Computing and printing remaining terms
for (let 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);
document.write(term + " ");
}
}
// main function started
let A = 3, X = 4, n = 5;
series(A, X, n);
// This code is contributed by Surbhi Tyagi.
</script>
Output: 243 1620 4320 5760 3840 1024
Time complexity : O(n)
Auxiliary Space : O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem