Answer to Question #284723 in C++ for papi

Question #284723

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





1
Expert's answer
2022-01-07T15:59:10-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog