Pyramid Star Pattern: flowchart, algorithm and program
This program reads n and prints a centred pyramid of stars with n rows. Each row first prints some spaces to push the stars to the right, then prints an odd number of stars, so the shape widens evenly on both sides.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read n.
- Step 3: Set i = 1.
- Step 4: If i <= n, go to step 5; otherwise go to step 15.
- Step 5: Set j = 1.
- Step 6: If j <= n - i, go to step 7; otherwise go to step 9.
- Step 7: Print space.
- Step 8: Increase j by 1. Go to step 6.
- Step 9: Set j = 1.
- Step 10: If j <= 2*i - 1, go to step 11; otherwise go to step 13.
- Step 11: Print *.
- Step 12: Increase j by 1. Go to step 10.
- Step 13: Print newline.
- Step 14: Increase i by 1. Go to step 4.
- Step 15: Stop.
Pseudocode
BEGIN
READ n
i ← 1
WHILE i <= n DO
j ← 1
WHILE j <= n - i DO
PRINT space
j++
END WHILE
j ← 1
WHILE j <= 2*i - 1 DO
PRINT *
j++
END WHILE
PRINT newline
i++
END WHILE
END
Program in C, Python and Java
Pyramid Star Pattern program in C
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}Pyramid Star Pattern program in Python
n = int(input("Enter n: "))
for i in range(1, n + 1):
for j in range(1, n - i + 1):
print(" ", end="")
for j in range(1, 2 * i):
print("*", end="")
print()Pyramid Star Pattern program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
sc.close();
}
}
Sample input: n = 4
Output: *
***
*****
*******
How it works
- The outer loop runs i from 1 to n, and each pass prints one row of the pyramid.
- The first inner loop runs j from 1 to n - i and prints a space each time. Row 1 gets n-1 spaces and the last row gets 0, which centres the pyramid.
- The second inner loop runs j from 1 to 2 * i - 1 and prints a star each time. This gives 1, 3, 5, 7... stars, growing by 2 per row.
- After both inner loops finish, printf("\n") moves the cursor to the next line before i increases.
- For n = 4 the rows are: 3 spaces + 1 star, 2 spaces + 3 stars, 1 space + 5 stars, 0 spaces + 7 stars.
Dry run (trace table)
| Step | Line | Box | n | i | j | Output |
|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||
| 2 | 6 | Read n | 4 | Enter n: | ||
| 3 | 7 | i = 1 | 4 | 1 | ||
| 4 | 7 | i <= n? | 4 | 1 | ||
| 5 | 8 | j = 1 | 4 | 1 | 1 | |
| 6 | 8 | j <= n - i? | 4 | 1 | 1 | |
| 7 | 9 | Print space | 4 | 1 | 1 | |
| 8 | 8 | j++ | 4 | 1 | 2 | |
| 9 | 8 | j <= n - i? | 4 | 1 | 2 | |
| 10 | 9 | Print space | 4 | 1 | 2 | |
| 11 | 8 | j++ | 4 | 1 | 3 | |
| 12 | 8 | j <= n - i? | 4 | 1 | 3 | |
| 13 | 9 | Print space | 4 | 1 | 3 | |
| 14 | 8 | j++ | 4 | 1 | 4 | |
| 15 | 8 | j <= n - i? | 4 | 1 | 4 | |
| 16 | 11 | j = 1 | 4 | 1 | 1 | |
| 17 | 11 | j <= 2*i - 1? | 4 | 1 | 1 | |
| 18 | 12 | Print * | 4 | 1 | 1 | * |
| 19 | 11 | j++ | 4 | 1 | 2 | |
| 20 | 11 | j <= 2*i - 1? | 4 | 1 | 2 |
Time and space complexity
Time: O(n²) · Space: O(1). The outer loop runs n times and each row prints about n characters in the inner loops; only a few variables are used.
Common mistakes
- Using i stars instead of 2 * i - 1, which gives a left-leaning triangle instead of a symmetric pyramid.
- Forgetting the space loop or writing it as n - i + 1, which shifts the pyramid and breaks the centring.
- Forgetting the newline after the star loop, so all rows print on one line.
Viva questions
Q1. What does this program do, and what is the role of the two inner loops?
It prints a centered pyramid of n rows. For each row, the first inner loop prints n-i spaces for alignment and the second prints 2i-1 stars.
Q2. Why does the first inner loop run up to n - i, and what happens on the last row?
Each row needs fewer leading spaces as it goes down, so that the stars stay centered. On the last row i = n, so n-i is 0, the loop does not run, and no spaces are printed.
Q3. Why is the number of stars 2*i - 1 and not just i?
The stars must grow by one on each side of the center, so each row has two more stars than the previous one: 1, 3, 5, 7... That is the odd-number sequence 2i-1. With only i stars you would get a left-aligned triangle instead of a pyramid.
Q4. Why is j reused in both inner loops, and why is it reset to 1 each time?
The first loop has finished before the second one starts, so the two do not clash and the same variable can be reused. The for statement sets j = 1 at its start, so each loop begins counting from the beginning.
Q5. What is the purpose of printf("\n") on line 14, and what would the output look like without it?
It ends the current row and moves the cursor to the next line. Without it, all the rows would run together on a single line.
Q6. What happens if the user enters n = 1, n = 0 or a negative number?
For n = 1 it prints a single star. For 0 or a negative number the outer loop condition i <= n is false at the start, so nothing is printed and the program just exits. There is no input validation.
Q7. What is the time complexity of this program?
Row i prints (n-i) + (2i-1) = n+i-1 characters. Summed over n rows this is proportional to n squared, so the time complexity is O(n^2).
Q8. What would you change to print an inverted pyramid, or to make a hollow pyramid?
For an inverted pyramid, run the outer loop from n down to 1 (i = n; i >= 1; i--) and keep the same inner loops. For a hollow one, print a star only at the first and last position of each row, and on the last row, and print spaces elsewhere.
Frequently asked questions
Why are there 2i-1 stars in row i?
The star count must go 1, 3, 5, 7... so the pyramid is symmetric around its centre. Each row adds one star on the left and one on the right, which is 2i-1 in total.
Why do we print n - i spaces before the stars?
The top row needs the most leading spaces to sit in the middle, and each lower row needs one less. n - i starts at n-1 for row 1 and falls to 0 for row n.
How do I print an inverted pyramid?
Run the outer loop from i = n down to 1 instead of up. The same space and star formulas then draw the widest row first and the single star last.