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:

#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();
}

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

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.
2 1 vote
Article Rating
Subscribe
Notify of
guest
5.4K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
5.4K
0
Would love your thoughts, please comment.x
()
x