Pascal’s Triangle
The Pascal triangle can be used to compute the coefficients of the terms in the expansion
(a + b)n. For example, (a + b)2 = a2 + 2ab + b2 where 1, 2, and 1 are coefficients. Write a C
program that creates a two-dimensional matrix a representing the Pascal triangle of size n. For
example, a Pascal triangle of size 10 is shown below
#include <bits/stdc++.h>
using namespace std;
void printPascal(int n)
{
for (int line = 1; line <= n; line++)
{
int C = 1; // used to represent C(line, i)
for (int i = 1; i <= line; i++)
{
// The first value in a line is always 1
cout<< C<<" ";
C = C * (line - i) / i;
}
cout<<"\n";
}
}
// Driver code
int main()
{
int n = 5;
printPascal(n);
return 0;
}
Comments
Leave a comment