Print a Half Pyramid Number Pattern in C
To print a half pyramid number pattern in C, we use nested loops. The outer loop controls the rows, while the inner loop prints numbers in increasing order. Each row contains numbers from 1 up to the row index, forming a triangular pattern.
Examples of Printing Half Pyramid Number Pattern
1. Printing a Simple Half Pyramid Number Pattern
In this example, we will print a half pyramid pattern using numbers. The rows will contain increasing numbers, starting from 1 in the first row and adding one more number in each subsequent row.
main.c
#include <stdio.h>
int main() {
int rows = 5;
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Inner loop for numbers
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n"); // Move to next line after each row
}
return 0;
}
Explanation:
- We declare an integer variable
rows
to define the number of rows in the pyramid. - The outer loop (
for (int i = 1; i <= rows; i++)
) controls the number of rows in the pattern. - The inner loop (
for (int j = 1; j <= i; j++)
) prints numbers from 1 to the row indexi
. Refer C For Loop. - The
printf("%d ", j)
statement prints the numbers with a space. - After printing each row,
printf("\n")
moves to the next line.
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
2. Printing a Half Pyramid with Repeated Row Numbers
In this example, instead of printing numbers from 1 to the row index, we will print the row index itself multiple times.
main.c
#include <stdio.h>
int main() {
int rows = 5;
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Inner loop for numbers
for (int j = 1; j <= i; j++) {
printf("%d ", i);
}
printf("\n"); // Move to next line after each row
}
return 0;
}
Explanation:
- We declare
rows = 5
to define the number of rows. - The outer loop (
for (int i = 1; i <= rows; i++)
) controls the number of rows. - The inner loop (
for (int j = 1; j <= i; j++)
) repeatsi
for each column. - The
printf("%d ", i)
statement prints the current row number. - The
printf("\n")
statement moves to the next line.
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
3. Printing an Inverted Half Pyramid of Numbers
In this example, we will print an inverted half pyramid, where the highest row number appears first, decreasing row by row.
main.c
#include <stdio.h>
int main() {
int rows = 5;
// Outer loop for rows
for (int i = rows; i >= 1; i--) {
// Inner loop for numbers
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n"); // Move to next line after each row
}
return 0;
}
Explanation:
- We declare
rows = 5
to define the number of rows. - The outer loop starts from
rows
and decrements down to 1. - The inner loop prints numbers from 1 up to the current row number
i
. - The
printf("%d ", j)
statement prints the numbers. - The
printf("\n")
moves to the next line.
Output:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Conclusion
In this tutorial, we covered how to print half pyramid number patterns using nested loops:
- A simple half pyramid where numbers increase from 1.
- A variation where each row contains repeated numbers.
- An inverted half pyramid where rows decrease in size.