How to resolve the algorithm Pascal's triangle step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pascal's triangle step by step in the PL/I programming language

Table of Contents

Problem Statement

Pascal's triangle is an arithmetic and geometric figure often associated with the name of Blaise Pascal, but also studied centuries earlier in India, Persia, China and elsewhere. Its first few rows look like this: where each element of each row is either 1 or the sum of the two elements right above it. For example, the next row of the triangle would be: So the triangle now looks like this: Each row   n   (starting with row   0   at the top) shows the coefficients of the binomial expansion of   (x + y)n.

Write a function that prints out the first   n   rows of the triangle   (with   f(1)   yielding the row consisting of only the element 1). This can be done either by summing elements from the previous rows or using a binary coefficient or combination function. Behavior for   n ≤ 0   does not need to be uniform, but should be noted.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pascal's triangle step by step in the PL/I programming language

Source code in the pl/i programming language

declare (t, u)(40) fixed binary;
declare (i, n) fixed binary;

t,u = 0;
get (n);
if n <= 0 then return;

do n = 1 to n;
   u(1) = 1;
   do i = 1 to n;
      u(i+1) = t(i) + t(i+1);
   end;
   put skip edit ((u(i) do i = 1 to n)) (col(40-2*n), (n+1) f(4));
   t = u;
end;

                                        1
                                      1   1
                                    1   2   1
                                  1   3   3   1
                                1   4   6   4   1
                              1   5  10  10   5   1
                            1   6  15  20  15   6   1
                          1   7  21  35  35  21   7   1
                        1   8  28  56  70  56  28   8   1
                      1   9  36  84 126 126  84  36   9   1
                    1  10  45 120 210 252 210 120  45  10   1

  

You may also check:How to resolve the algorithm Quine step by step in the Draco programming language
You may also check:How to resolve the algorithm Exceptions step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Ol programming language
You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the Julia programming language