Half Pyramid Pattern Program in C
Print a half pyramid (right-angled triangle) of stars and of numbers in C using nested for loops.
- Time
O(n^2), since the inner loop runs 1 + 2 + ... + n = n(n + 1) / 2 times - Space
O(1) - Hints3
- Read4 min
A half pyramid in C uses two nested for loops. The outer loop runs i from 1 to rows. The inner loop runs j from 1 to i and prints "* ". After the inner loop, printf("\n") ends the row. Row 1 gets one star, row 5 gets five. Print j instead of a star for 1, 1 2, 1 2 3, or print i for 1, 2 2, 3 3 3.
Problem statement
Write a half pyramid pattern program in C. Given the number of rows, print a
right-angled triangle of stars. For rows = 5:
*
* *
* * *
* * * *
* * * * *
Then change the program to print numbers instead of stars.
An integer rows.
rows lines; line i has i items.
Examples
rows = 3, stars"*", "* *", "* * *"Row i has i stars.
rows = 3, numbers"1", "1 2", "1 2 3"Row i prints the numbers 1 to i.
Constraints
- 1 <= rows <= 50
Think before you code
Reveal the questions to ask yourself first
- How many stars does row 3 have? Row 4? What is the rule?
- Which loop decides how many lines are printed, and which decides how long each line is?
- Where does printf("\n") go so that each row ends on its own line?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
Nested loops for a right triangle pattern in C
Every pattern program has the same shape: the outer loop counts rows, the inner loop prints the items in one row, and a newline ends each row.
| Row i | Inner loop j runs | Printed |
|---|---|---|
| 1 | 1 | * |
| 2 | 1, 2 | * * |
| 3 | 1, 2, 3 | * * * |
| 4 | 1 to 4 | * * * * |
| 5 | 1 to 5 | * * * * * |
The inner loop's limit is i, the current row. That one link between the two loops
is what makes the triangle grow.
What the inner loop prints
| Pattern | Inner printf |
Row 3 looks like |
|---|---|---|
| Stars | printf("* "); |
* * * |
| Numbers 1 to i | printf("%d ", j); |
1 2 3 |
| Row number repeated | printf("%d ", i); |
3 3 3 |
| Floyd's triangle | printf("%d ", k++); |
4 5 6 |
The loops stay the same. Only the item changes. For more on nested loops, see loops in programming.
Dry run
Walk through the example step by step
rows = 3, star version:
i | j values | output on this row | then
--+----------+--------------------+--------------
1 | 1 | "* " | printf("\n")
2 | 1, 2 | "* * " | printf("\n")
3 | 1, 2, 3 | "* * * " | printf("\n")
4 | - | outer loop ends (4 <= 3 is false)
The inner loop runs 1 + 2 + 3 = 6 times in total.
Solution
Reveal the full C solution
#include <stdio.h>
int main(void) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}Output:
*
* *
* * *
* * * *
* * * * *
Each star is printed as "* ", with a space after it, so the rows are easier to read.
Use printf("*") if you want the stars touching.
Number half pyramid in C
#include <stdio.h>
int main(void) {
int rows = 5;
printf("Numbers 1 to i:\n");
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
printf("Row number repeated:\n");
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", i);
}
printf("\n");
}
printf("Floyd's triangle:\n");
int k = 1;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", k);
k++;
}
printf("\n");
}
return 0;
}
Numbers 1 to i:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Row number repeated:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Floyd's triangle:
1
2 3
4 5 6
7 8 9 10
- Printing
jgives the column number, so every row starts again from 1. - Printing
igives the row number, repeateditimes. - In Floyd's triangle,
kis declared outside both loops, so it never resets and keeps counting across rows.
Inverted and right-aligned half pyramids
#include <stdio.h>
int main(void) {
int rows = 5;
printf("Inverted half pyramid:\n");
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
printf("Right-aligned half pyramid:\n");
for (int i = 1; i <= rows; i++) {
for (int s = 1; s <= rows - i; s++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Inverted half pyramid:
* * * * *
* * * *
* * *
* *
*
Right-aligned half pyramid:
*
* *
* * *
* * * *
* * * * *
- The inverted version only changes the outer loop to count down from
rowsto 1. - The right-aligned version adds a third loop that prints
rows - iblocks of two spaces before the stars. Each block is two spaces wide because each"* "is two characters wide.
With user input
#include <stdio.h>
int main(void) {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Enter number of rows: 4
*
**
***
****
For a centred pyramid, see pyramid pattern program in Python; the same formulas work in C. The Java versions are on star pattern programs in Java. For other loop exercises in C, see simple loop programs in C.
O(n^2), since the inner loop runs 1 + 2 + ... + n = n(n + 1) / 2 timesO(1)Review before the interview
Common mistakes
- Using j <= rows in the inner loop, which prints a square instead of a triangle.
- Putting printf("\n") inside the inner loop, so every star lands on its own line.
- Putting printf("\n") outside the outer loop, so all stars print on one line.
- Declaring k inside the outer loop in Floyd's triangle, which resets it to 1 on every row.
- Using one space per block in the right-aligned version while each star takes two characters, so it leans.
Edge cases to test
- rows = 1: a single star.
- rows = 0 or negative: the outer loop does not run and nothing is printed.
- Rows of 10 or more in the number version: two-digit numbers make later rows wider, so columns shift.
- Each line ends with a trailing space from "* ". This is usually fine, but some online judges reject it.
Interview follow-ups
- How would you print a hollow half pyramid with stars only on the edges?
- How would you print the alphabet half pyramid A, A B, A B C?
- How would you combine a half pyramid and an inverted one into an arrow shape?
Keep practising
Pyramid Pattern Program in Python
Hints · Dry run · SolutionSolve Similar problemStar Pattern Programs in Java
Hints · Dry run · SolutionSolve Similar problemSimple Loop Programs in C for Practice
Hints · Dry run · SolutionSolve Similar problemPascal Triangle in Java
Hints · Dry run · SolutionSolve Related learningLoops In Programming
TutorialLearn Related learningLearn C Programming Step By Step
TutorialLearn Related learningC Programming Interview Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartBuilding your programming basics?
CodeBegun’s programs build problem-solving with mentor review and mock interviews, online or in Madhapur, Hyderabad.
