Skip to content

Commit c13bc99

Browse files
authored
Merge pull request zapellass123#461 from mishracode11/patch-2
Trinomial_Triangle.cpp
2 parents 032dc40 + c667321 commit c13bc99

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Trinomial_Triangle.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

0 commit comments

Comments
 (0)