C Program to Print Pyramid Numbers Pattern

Write a C program to print a pyramid numbers pattern using for loop or a program to print a pyramid pattern of numbers.

#include <stdio.h>

int main()
{
	int rows;

	printf("Enter Pyramid Number Pattern Rows = ");
	scanf("%d", &rows);

	printf("Printing Pyramid Number Pattern\n");

	for (int i = 1; i <= rows; i++)
	{
		for (int j = rows; j > i; j--)
		{
			printf(" ");
		}
		for (int k = 1; k <= i; k++)
		{
			printf("%d ", i);
		}
		printf("\n");
	}
}
C Program to Print Pyramid Numbers Pattern using for loop

This C program helps to print a pyramid pattern of numbers using a while loop.

#include <stdio.h>

int main()
{
	int i, j, k, rows;

	printf("Enter Pyramid Number Pattern Rows = ");
	scanf("%d", &rows);

	printf("\n");
	
	i = 1;

	while (i <= rows)
	{
		j = rows;
		while (j > i)
		{
			printf(" ");
			j--;
		}

		k = 1;
		while (k <= i)
		{
			printf("%d ", i);
			k++;
		}

		printf("\n");
		i++;
	}
}
Enter Pyramid Number Pattern Rows = 8

       1 
      2 2 
     3 3 3 
    4 4 4 4 
   5 5 5 5 5 
  6 6 6 6 6 6 
 7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8

This example uses the do while loop to print the pyramid pattern of numbers.

#include <stdio.h>

int main()
{
	int i, j, k, rows;

	printf("Enter Rows = ");
	scanf("%d", &rows);

	printf("\n");
	
	i = 1;

	do
	{
		j = rows;
		do
		{
			printf(" ");

		} while (j-- > i);

		k = 1;
		do
		{
			printf("%d ", i);

		} while (++k <= i);

		printf("\n");

	} while (++i <= rows);
}
Enter Rows = 9

         1 
        2 2 
       3 3 3 
      4 4 4 4 
     5 5 5 5 5 
    6 6 6 6 6 6 
   7 7 7 7 7 7 7 
  8 8 8 8 8 8 8 8 
 9 9 9 9 9 9 9 9 9 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.