To print Pascal triangle using numbers

To print Pascal triangle using numbers

This program is used to print the Pascal triangle using numbers.

Logic:

  • Get the number of rows from the user.
  • Then print the corresponding spaces for each row.
  • Check the position whether it is a boundary or intermediate.
  • If it is boundary prints the value one.
  • Then other positions are printed by adding two elements in the upper row.

Program:

[code lang=”c”]
#include<stdio.h>
void main()
{
int i,j,a,n,k;
clrscr();
printf("enter the row:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
if(j==0||j==i)//prints first and last element in all rows
{
a=1;
}
else//it prints the intermediate values
{
a=a*(i-j+1)/j;
}
printf("%d ",a);
}printf("\n");
}
getch();
}
[/code]

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

Leave a Reply

Your email address will not be published. Required fields are marked *