In this tutorial, we will learn and write codes for various half pyramids number patterns in C programming language. However, in this tutorial, we will create a numeric pattern in C using for loop. So you may go through the following topic in C.
- for loop in C : loops plays a key role to achieve various patterns in any programming language. Mastering patterns means having excellent grasp at loops.
Left Half Pyramids in C using Numbers
We will look at the following topic of pyramid patterns in C. Each of the programs is consists of different number patterns.
- Left half pyramid
- Inverted left half pyramid
Left half pyramid pattern of numbers in C
Pattern 1:
#include <stdio.h>
int main()
{
int i, j, k, rows;
printf("Enter the no. of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = i; j < rows; j++)
printf(" ");
for (k = 1; k <= i; k++)
printf(" %d", k);
printf("\n");
}
return 0;
}
Output:
Enter the no. of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Inverted left half pyramid patterns of Numbers in C
Pattern 1:
#include <stdio.h>
int main()
{
int i, j, k, rows;
printf("Enter the no. of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
printf(" ");
for (k = i; k <= rows; k++)
printf(" %d", k);
printf("\n");
}
return 0;
}
Output:
Enter the no. of rows: 5
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5