The trinomial triangle is a variation of Pascal’s triangle. The difference between the two is that an entry in the trinomial triangle is the sum of the three (rather than the two in Pascal’s triangle) entries above it :

The k-th entry of the n-th row is denoted by :

Rows are counted starting from 0. The entries of the n-th row are indexed starting with -n from the left, and the middle entry has index 0. The symmetry of the entries of a row about the middle entry is expressed by the relationship

Properties :
- The n-th row corresponds to the coefficients in the polynomial expansion of the expansion of the trinomial (1 + x + x2) raised to the n-th power.

or Symmetrically

hence the alternative name trinomial coefficients because of their relationship to the multinomial coefficients :

- The diagonals have intersecting properties, such as their relationship to the triangular numbers.
- The sum of the elements of n-th row is 3n.
Recursion formula
The trinomial coefficients can be generated using the following recursion formula :


where,
, for k n
Applications :
- The triangle corresponds to the number of possible paths that can be taken by the king in a game of chess. The entry in a cell represents the number of different paths (using minimum number of moves) the king can take to reach the cell.

- The coefficient of xk in the polynomial (1 + x + x2)n specifies the number of different ways of randomly drawing k cards from two sets of n identical playing cards. For example, in such a card game with two sets of the three cards A, B, C , the choices look like this :

- Given a positive number n. The task is to print Trinomial Triangle of height n.
Examples:
Input : n = 4
Output :
1
1 1 1
1 2 3 2 1
1 3 6 7 6 3 1
Input : n = 5
Output :
1
1 1 1
1 2 3 2 1
1 3 6 7 6 3 1
1 4 10 16 19 16 10 4 1
- Below is the implementation of printing trinomial triangle of height n :
C++
#include<bits/stdc++.h>
using namespace std;
int TrinomialValue( int n, int k)
{
if (n == 0 && k == 0)
return 1;
if (k < -n || k > n)
return 0;
return TrinomialValue (n - 1, k - 1)
+ TrinomialValue (n - 1, k)
+ TrinomialValue (n - 1, k + 1);
}
void printTrinomial( int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = -i; j <= 0; j++)
cout << TrinomialValue(i, j) << " " ;
for ( int j = 1; j <= i; j++)
cout << TrinomialValue(i, j) << " " ;
cout << endl;
}
}
int main()
{
int n = 4;
printTrinomial(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG {
public static int TrinomialValue( int n,
int k)
{
if (n == 0 && k == 0 )
return 1 ;
if (k < -n || k > n)
return 0 ;
return TrinomialValue(n - 1 , k - 1 )
+ TrinomialValue(n - 1 , k)
+ TrinomialValue(n - 1 , k + 1 );
}
public static void printTrinomial( int n)
{
for ( int i = 0 ; i < n; i++)
{
for ( int j = -i; j <= 0 ; j++)
System.out.print(TrinomialValue(i, j)
+ " " );
for ( int j = 1 ; j <= i; j++)
System.out.print(TrinomialValue(i, j)
+ " " );
System.out.println();
}
}
public static void main(String argc[])
{
int n = 4 ;
printTrinomial(n);
}
}
|
Python3
def TrinomialValue(n, k):
if n = = 0 and k = = 0 :
return 1
if k < - n or k > n:
return 0
return (TrinomialValue (n - 1 , k - 1 ) +
TrinomialValue (n - 1 , k) +
TrinomialValue (n - 1 , k + 1 ))
def printTrinomial( n ):
for i in range (n):
for j in range ( - i, 1 ):
print (TrinomialValue(i, j),end = " " )
for j in range ( 1 , i + 1 ):
print ( TrinomialValue(i, j),end = " " )
print ( "\n" ,end = '')
n = 4
printTrinomial(n)
|
C#
using System;
public class GfG {
public static int TrinomialValue( int n,
int k)
{
if (n == 0 && k == 0)
return 1;
if (k < -n || k > n)
return 0;
return TrinomialValue(n - 1, k - 1)
+ TrinomialValue(n - 1, k)
+ TrinomialValue(n - 1, k + 1);
}
public static void printTrinomial( int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = -i; j <= 0; j++)
Console.Write(TrinomialValue(i, j)
+ " " );
for ( int j = 1; j <= i; j++)
Console.Write(TrinomialValue(i, j)
+ " " );
Console.WriteLine();
}
}
public static void Main()
{
int n = 4;
printTrinomial(n);
}
}
|
PHP
<?php
function TrinomialValue( $n , $k )
{
if ( $n == 0 && $k == 0)
return 1;
if ( $k < - $n || $k > $n )
return 0;
return TrinomialValue ( $n - 1, $k - 1) +
TrinomialValue ( $n - 1, $k ) +
TrinomialValue ( $n - 1, $k + 1);
}
function printTrinomial( $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = - $i ; $j <= 0; $j ++)
echo TrinomialValue( $i , $j ), " " ;
for ( $j = 1; $j <= $i ; $j ++)
echo TrinomialValue( $i , $j ) , " " ;
echo "\n" ;
}
}
$n = 4;
printTrinomial( $n );
?>
|
Javascript
<script>
function TrinomialValue(n, k)
{
if (n == 0 && k == 0)
return 1;
if (k < -n || k > n)
return 0;
return TrinomialValue(n - 1, k - 1)
+ TrinomialValue(n - 1, k)
+ TrinomialValue(n - 1, k + 1);
}
function printTrinomial(n)
{
for (let i = 0; i < n; i++)
{
for (let j = -i; j <= 0; j++)
document.write(TrinomialValue(i, j)
+ " " );
for (let j = 1; j <= i; j++)
document.write(TrinomialValue(i, j)
+ " " );
document.write( "<br/>" );
}
}
let n = 4;
printTrinomial(n);
</script>
|
Output:
1
1 1 1
1 2 3 2 1
1 3 6 7 6 3 1
- Below is the implementation of printing Trinomial Triangle using Dynamic Programming and property of trinomial triangle i.e

C++
#include <bits/stdc++.h>
#define MAX 10
using namespace std;
int TrinomialValue( int dp[MAX][MAX], int n, int k)
{
if (k < 0)
k = -k;
if (dp[n][k] != 0)
return dp[n][k];
if (n == 0 && k == 0)
return 1;
if (k < -n || k > n)
return 0;
return (dp[n][k] = TrinomialValue(dp, n - 1, k - 1)
+ TrinomialValue(dp, n - 1, k)
+ TrinomialValue(dp, n - 1, k + 1));
}
void printTrinomial( int n)
{
int dp[MAX][MAX] = { 0 };
for ( int i = 0; i < n; i++) {
for ( int j = -i; j <= 0; j++)
cout << TrinomialValue(dp, i, j) << " " ;
for ( int j = 1; j <= i; j++)
cout << TrinomialValue(dp, i, j) << " " ;
cout << endl;
}
}
int main()
{
int n = 4;
printTrinomial(n);
return 0;
}
|
C
#include<bits/stdc++.h>
#define MAX 10
using namespace std;
int TrinomialValue( int dp[MAX][MAX], int n, int k)
{
if (k < 0)
k = -k;
if (dp[n][k] != 0)
return dp[n][k];
if (n == 0 && k == 0)
return 1;
if (k < -n || k > n)
return 0;
return (dp[n][k] = TrinomialValue(dp, n - 1, k - 1)
+ TrinomialValue(dp, n - 1, k)
+ TrinomialValue(dp, n - 1, k + 1));
}
void printTrinomial( int n)
{
int dp[MAX][MAX] = { 0 };
for ( int i = 0; i < n; i++)
{
for ( int j = -i; j <= 0; j++)
cout << TrinomialValue(dp, i, j) << " " ;
for ( int j = 1; j <= i; j++)
cout << TrinomialValue(dp, i, j) << " " ;
cout << endl;
}
}
int main()
{
int n = 4;
printTrinomial(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG {
private static final int MAX = 10 ;
public static int TrinomialValue( int dp[][], int n, int k)
{
if (k < 0 )
k = -k;
if (dp[n][k] != 0 )
return dp[n][k];
if (n == 0 && k == 0 )
return 1 ;
if (k < -n || k > n)
return 0 ;
return (dp[n][k] = TrinomialValue(dp, n - 1 , k - 1 )
+ TrinomialValue(dp, n - 1 , k)
+ TrinomialValue(dp, n - 1 , k + 1 ));
}
public static void printTrinomial( int n)
{
int [][] dp = new int [MAX][MAX];
for ( int i = 0 ; i < n; i++) {
for ( int j = -i; j <= 0 ; j++)
System.out.print(TrinomialValue(dp, i, j) + " " );
for ( int j = 1 ; j <= i; j++)
System.out.print(TrinomialValue(dp, i, j) + " " );
System.out.println();
}
}
public static void main(String argc[])
{
int n = 4 ;
printTrinomial(n);
}
}
|
Python3
def TrinomialValue(dp , n , k):
if k < 0 :
k = - k
if dp[n][k] ! = 0 :
return dp[n][k]
if n = = 0 and k = = 0 :
return 1
if k < - n or k > n:
return 0
return (TrinomialValue(dp, n - 1 , k - 1 ) +
TrinomialValue(dp, n - 1 , k) +
TrinomialValue(dp, n - 1 , k + 1 ))
def printTrinomial(n):
dp = [[ 0 ] * 10 ] * 10
for i in range (n):
for j in range ( - i, 1 ):
print (TrinomialValue(dp, i, j),end = " " )
for j in range ( 1 ,i + 1 ):
print (TrinomialValue(dp, i, j),end = " " )
print ( "\n" ,end = '')
n = 4
printTrinomial(n)
|
C#
using System;
class GFG
{
private static int MAX = 10;
public static int TrinomialValue( int [,]dp,
int n, int k)
{
if (k < 0)
k = -k;
if (dp[n, k] != 0)
return dp[n, k];
if (n == 0 && k == 0)
return 1;
if (k < -n || k > n)
return 0;
return (dp[n, k] = TrinomialValue(dp, n - 1,
k - 1) +
TrinomialValue(dp, n - 1,
k) +
TrinomialValue(dp, n - 1,
k + 1));
}
public static void printTrinomial( int n)
{
int [,] dp = new int [MAX, MAX];
for ( int i = 0; i < n; i++)
{
for ( int j = -i; j <= 0; j++)
Console.Write(TrinomialValue(dp, i,
j) + " " );
for ( int j = 1; j <= i; j++)
Console.Write(TrinomialValue(dp, i,
j) + " " );
Console.WriteLine();
}
}
static public void Main ()
{
int n = 4;
printTrinomial(n);
}
}
|
PHP
<?php
$MAX = 10;
function TrinomialValue( $dp , $n , $k )
{
if ( $k < 0)
$k = - $k ;
if ( $dp [ $n ][ $k ] != 0)
return $dp [ $n ][ $k ];
if ( $n == 0 && $k == 0)
return 1;
if ( $k < - $n || $k > $n )
return 0;
return ( $dp [ $n ][ $k ] = TrinomialValue( $dp , $n - 1, $k - 1) +
TrinomialValue( $dp , $n - 1, $k ) +
TrinomialValue( $dp , $n - 1, $k + 1));
}
function printTrinomial( $n )
{
global $MAX ;
$dp ;
for ( $i = 0; $i < $MAX ; $i ++)
for ( $j = 0; $j < $MAX ; $j ++)
$dp [ $i ][ $j ] = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = - $i ; $j <= 0; $j ++)
echo TrinomialValue( $dp , $i , $j ). " " ;
for ( $j = 1; $j <= $i ; $j ++)
echo TrinomialValue( $dp , $i , $j ). " " ;
echo "\n" ;
}
}
$n = 4;
printTrinomial( $n );
?>
|
Javascript
<script>
var MAX = 10
function TrinomialValue(dp, n, k)
{
if (k < 0)
k = -k;
if (dp[n][k] != 0)
return dp[n][k];
if (n == 0 && k == 0)
return 1;
if (k < -n || k > n)
return 0;
return (dp[n][k] = TrinomialValue(dp, n - 1, k - 1)
+ TrinomialValue(dp, n - 1, k)
+ TrinomialValue(dp, n - 1, k + 1));
}
function printTrinomial(n)
{
var dp = Array.from(Array(MAX), ()=> Array(MAX).fill(0));
for ( var i = 0; i < n; i++)
{
for ( var j = -i; j <= 0; j++)
document.write( TrinomialValue(dp, i, j) + " " );
for ( var j = 1; j <= i; j++)
document.write( TrinomialValue(dp, i, j) + " " );
document.write( "<br>" );
}
}
var n = 4;
printTrinomial(n);
</script>
|
Output:
1
1 1 1
1 2 3 2 1
1 3 6 7 6 3 1
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
02 Jun, 2022
Like Article
Save Article