## Q. Write An Algorithm And Program 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.

#### Algorithm To Print Pascal's Triangle

```\\Algorithm To 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
```

#### Program To Print Pascal's Triangle

• C
• C++
• Java
• Python
• C#
• PHP
• ```//C Program To Print Pascal's Triangle
#include <stdio.h>
int main() {
int rows, coef = 1, space, i, j;
//Enter the number of rows
printf("Enter the number of rows: ");
scanf("%d", &rows);
//Outer loop
for (i = 0; i < rows; i++) {
//Inner Loop 1 to give space
for (space = 1; space <= rows - i; space++)
printf("  ");
//Inner Loop 2 to calculate coef
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
```
```//C++ Program To Print Pascal's Triangle
#include <iostream>
using namespace std;
int main()
{
int rows, coef = 1, space, i, j;
//Enter the number of rows
cout<<"Enter the number of rows: ";
cin>>rows;
//Outer loop
for (i = 0; i < rows; i++) {
//Inner Loop 1 to give space
for (space = 1; space <= rows - i; space++)
cout<<"  ";
//Inner Loop 2 to calculate coef
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
cout<<" "<<coef<<" ";
}
cout<<"\n";
}
return 0;
}
```
```//Java Program To Print Pascal's Triangle
public class LFC
{
public static void main(String[] args) {
int coef = 1, space, i, j;
//Enter the number of rows
int rows = 6;
//Outer loop
for (i = 0; i < rows; i++) {
//Inner Loop 1 to give space
for (space = 1; space <= rows - i; space++)
System.out.print(" ");
//Inner Loop 2 to calculate coef
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
System.out.print(" "+coef+" ");
}
System.out.println();
}
}
}
```
```#Python Program To Print Pascal's Triangle
from math import factorial
# Enter the number of rows
rows = 6
for i in range(rows):
# Inner Loop 1 to give space
for space in range(rows-i+1):
print(end=" ")
# Inner Loop 2 for calculation
for j in range(i+1):
# nCr = n!/((n-r)!*r!)
print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")
# for new line
print()
```
```//C# Program To Print Pascal's Triangle
using System;
class LFC {
static void Main() {
int coef = 1, space, i, j;
//Enter the number of rows
int rows = 6;
//Outer loop
for (i = 0; i < rows; i++) {
//Inner Loop 1 to give space
for (space = 1; space <= rows - i; space++)
Console.Write(" ");
//Inner Loop 2 to calculate coef
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
Console.Write(" "+coef+" ");
}
Console.WriteLine();
}
}
}
```
```<?php
//PHP Program To Print Pascal's Triangle
//Enter the number of rows
\$rows = 6;
\$coef = 1;
//Outer loop
for (\$i = 0; \$i < \$rows; \$i++) {
echo "\t";
//Inner Loop 1 to give space
for (\$space = 1; \$space <= \$rows - \$i; \$space++) {
echo "  ";
}
//Inner Loop 2 to calculate coef
for (\$j = 0; \$j <= \$i; \$j++) {
if (\$j == 0 || \$i == 0)
\$coef = 1;
else
\$coef = \$coef * (\$i - \$j + 1) / \$j;
echo \$coef . "   ";
}
echo "\n";
}
```
```//C Program To Print Pascal's Triangle
#include <stdio.h>
int main() {
int rows, coef = 1, space, i, j;
//Enter the number of rows
printf("Enter the number of rows: ");
scanf("%d", &rows);
//Outer loop
for (i = 0; i < rows; i++) {
//Inner Loop 1 to give space
for (space = 1; space <= rows - i; space++)
printf("  ");
//Inner Loop 2 to calculate coef
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
```

#### 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