Q18 Write a c program to print the Pascal Triangle on the screen.

Write a program to produce the following output:

Program: 111

Write a c program to print the Pascal Triangle on the screen.

               1 
           1       1
       1       2        1
   1       3        3       1
1      4       6        4        1
Pascal Triangle

#include<stdio.h>
#include<conio.h>
int main()
{
    int row, col, space,num;

    //lets create a loop for rows here we have 5 rows

    for(row=1;row<=5;row++)
    {
        //lets create space on left side
        for(space=1;space<=5-row;space++)
        {
            printf(" ");
        }

        //create a for loop for pattern
        for(col=1;col<=row;col++)
        {
            printf("%d ",fact(row, col));
        }

        printf("\n");
    }
    return 0;
}

//we are creating another function fact because we have to use recursion here

int fact(int i, int j)
{
    if(j == 1 || j == i)
        return 1;
    else
        return fact(i-1, j-1) + fact(i-1, j);
}
 

Output:

               1 
           1       1
       1       2        1
   1       3        3       1
1      4       6        4        1

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.
Related Post
Leave a Comment

This website uses cookies.