Q30. Write a code to print Pascal's triangle

Here you will find an Algorithm And Program to print Pascal’s Triangle. Given an integer value n and our task is to print first n lines of the Pascal’s triangle
Pascal’s Triangle : Pascal’s triangle is a triangular array of binomial coefficients. Pascal’s triangle is a type of number pattern. The numbers are so arranged that they are reflected as triangles. For Example :-

Input : Enter the number of rows: 6

Output:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Explanation : As we can see that input integer number : Num = 6. Output will be the Pascal’s Triangle.
Now let us understand what logic we need to use in this – In the program three loop are used. :
1. Outer loop will run throughout the program. The loop will start from i = 0 to rows.
2. Inner loop 1 is used to give space between the number. The loop will start from space = 1 to rows-i
3. Inner loop 2 is used to calculate the coef and print the coef. The inner loop 2 start from j= 0 to i.

Algoritham For Print Pascal’s Triangle

START
Step 1: [ Take Input ] Read: Number of rows
Step 2 : [Define] coef = 1, space, i, j.
Step 3: Loop start from i = 0 to rows:
Loop start from space = 1 to rows-i[for space between number]:
print space ” “
Loop end
Loop start from j= 0 to i:
[Check If Condition] if i or j equals 0
print 1
[Check Else Condition]
print coef * (i – j + 1) / j;
Loop end
Give one line space
Loop end
Stop

Code For Print Pascal’s Triangle

Output

Number of rows: 6
Output :
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number