File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ // CPP Program to print trinomial triangle.
2+ #include < bits/stdc++.h>
3+ using namespace std ;
4+
5+ // Function to find the trinomial triangle value.
6+ int TrinomialValue (int n, int k)
7+ {
8+ // base case
9+ if (n == 0 && k == 0 )
10+ return 1 ;
11+
12+ // base case
13+ if (k < -n || k > n)
14+ return 0 ;
15+
16+ // recursive step.
17+ return TrinomialValue (n - 1 , k - 1 )
18+ + TrinomialValue (n - 1 , k)
19+ + TrinomialValue (n - 1 , k + 1 );
20+ }
21+
22+ // Function to print Trinomial Triangle of height n.
23+ void printTrinomial (int n)
24+ {
25+ // printing n rows.
26+ for (int i = 0 ; i < n; i++)
27+ {
28+ // printing first half of triangle
29+ for (int j = -i; j <= 0 ; j++)
30+ cout << TrinomialValue (i, j) << " " ;
31+
32+ // printing second half of triangle.
33+ for (int j = 1 ; j <= i; j++)
34+ cout << TrinomialValue (i, j) << " " ;
35+
36+ cout << endl;
37+ }
38+ }
39+
40+ // Driven Program
41+ int main ()
42+ {
43+ int n = 4 ;
44+
45+ printTrinomial (n);
46+ return 0 ;
47+ }
You can’t perform that action at this time.
0 commit comments