;

C Program To Print the Pascal's Triangle


Tutorialsrack 03/11/2019 Programs C

In this C program, we will learn how to write a program to print the pascal's triangle.

Here is the code of the program to print the pascal's triangle.

Code - C Program To Print the Pascal's Triangle
/*
C Program To Print Pascal's Triangle
           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1 
*/

#include <stdio.h>
int main()
{
    int rows, coef = 1, space, i, j;
    printf("Enter The Number of Rows: ");
    scanf("%d",&rows);
    for(i=0; i<rows; i++)
    {
        for(space=1; space <= rows-i; space++)
            printf("  ");
        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

Enter The Number of Rows:
5
         1
       1  1
    1   2   1
 1   3   3  1
1  4  6  4  1

--------------------------------
Process exited after 4.117 seconds with return value 0
Press any key to continue . . .


Related Posts



Comments

Recent Posts
Tags