OCJP

Pattern Pyramid with Numbers in C

As we have learned simple patterns and complex patterns using for loop in our previous articles, now we are going to demonstrate complex numerical pattern pyramids. I advise to refer earlier posts in C Programming if you have not refereed yet for better understanding.

#include<stdio.h>
#include<conio.h>
void main()
{
	int i,j,k,sp=-1;
	clrscr();

	for(i=5;i>=1;i--)
	{
		//First Half of Pyramid
		for(j=1;j<=i;j++)
		{
		printf("%d ",j);
		}

		//Loop for Space No Space in First Row So sp is initialized with -1
		//From second line 2 increment in space

		for(j=1;j<=sp;j++)
		{
		printf("  ");//two spaces
		}
		sp+=2; //Two Increment in Space

		//Loop for Another Triangle
	//For first row we start with 5-1= 4 and for another rows 4,3,2
		if(i==5)
		j= i-1;
		else
		j=i;


		for(;j>=1;j--)//Initialization part is not placed
		{
		printf("%d ",j);
		}
	printf("\n");
	}
	getch();
}
/*
1 2 3 4 5 4 3 2 1
1 2 3 4   4 3 2 1
1 2 3       3 2 1
1 2           2 1
1               1
*/
Complex Pyramid Program with Numbers In C

As we have used Nesting of Loops . We can use any loop for the same programs. Even Without Loop using goto (Jump Statement) we can develop the same program. You can try the same and place the program in Comment Section.

Don’t forget to subscribe to get Latest Updated Programs.

Leave a Reply

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


× How can I help you?