Inverted Triangle Star Pattern: flowchart, algorithm and program
This program reads n and prints an inverted right triangle of stars, where the first row has n stars and each row after it has one less. It uses two nested loops: the outer loop picks the row, and the inner loop prints that row's stars.
▶ 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 11.
- Step 5: Set j = 1.
- Step 6: If j <= n - i + 1, go to step 7; otherwise go to step 9.
- Step 7: Print "*".
- Step 8: Increase j by 1. Go to step 6.
- Step 9: Print newline.
- Step 10: Increase i by 1. Go to step 4.
- Step 11: Stop.
Pseudocode
BEGIN
READ n
i ← 1
WHILE i <= n DO
j ← 1
WHILE j <= n - i + 1 DO
PRINT "*"
j++
END WHILE
PRINT newline
i++
END WHILE
END
Program in C, Python and Java
Inverted Triangle 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 + 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}Inverted Triangle Star Pattern program in Python
n = int(input("Enter n: "))
for i in range(1, n + 1):
for j in range(1, n - i + 2):
print("*", end="")
print()Inverted Triangle 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 + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Sample input: n = 5
Output: *****
****
***
**
*
How it works
- The outer loop runs i from 1 to n, so it prints exactly n rows.
- For each row, the inner loop runs j from 1 to n - i + 1, and each pass prints one '*'.
- Because n - i + 1 gets smaller as i grows, row 1 has n stars, row 2 has n-1, and the last row (i = n) has just 1.
- After the inner loop finishes, printf("\n") moves to the next line so the next row starts fresh.
- For n = 5 the star counts are 5, 4, 3, 2, 1, which gives the output *****, ****, ***, **, *.
Dry run (trace table)
| Step | Line | Box | n | i | j | Output |
|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||
| 2 | 6 | Read n | 5 | |||
| 3 | 7 | i = 1 | 5 | 1 | ||
| 4 | 7 | i <= n? | 5 | 1 | ||
| 5 | 8 | j = 1 | 5 | 1 | 1 | |
| 6 | 8 | j <= n - i + 1? | 5 | 1 | 1 | |
| 7 | 9 | Print "*" | 5 | 1 | 1 | * |
| 8 | 8 | j++ | 5 | 1 | 2 | |
| 9 | 8 | j <= n - i + 1? | 5 | 1 | 2 | |
| 10 | 9 | Print "*" | 5 | 1 | 2 | * |
| 11 | 8 | j++ | 5 | 1 | 3 | |
| 12 | 8 | j <= n - i + 1? | 5 | 1 | 3 | |
| 13 | 9 | Print "*" | 5 | 1 | 3 | * |
| 14 | 8 | j++ | 5 | 1 | 4 | |
| 15 | 8 | j <= n - i + 1? | 5 | 1 | 4 | |
| 16 | 9 | Print "*" | 5 | 1 | 4 | * |
| 17 | 8 | j++ | 5 | 1 | 5 | |
| 18 | 8 | j <= n - i + 1? | 5 | 1 | 5 | |
| 19 | 9 | Print "*" | 5 | 1 | 5 | * |
| 20 | 8 | j++ | 5 | 1 | 6 |
Time and space complexity
Time: O(n²) · Space: O(1). The inner loop runs n, n-1, ..., 1 times, about n(n+1)/2 prints in total, using only a few variables.
Common mistakes
- Using j <= n - i instead of n - i + 1, which prints one star too few in every row and leaves the last row empty.
- Forgetting the printf("\n") after the inner loop, so all the stars print on a single line.
- Placing the newline inside the inner loop, which prints each star on its own line instead of forming rows.
Viva questions
Q1. What does this program print for n = 4?
It prints 4 rows: 4 stars, then 3, then 2, then 1, so the triangle is upside down.
Q2. What is the role of the outer loop and the inner loop?
The outer loop with i controls the rows, running n times. The inner loop with j prints the stars in one row.
Q3. Why does the inner loop run up to n - i + 1 and not just n - i?
Row i must have n-i+1 stars. For i = 1 that gives n stars, and for the last row i = n it gives 1 star. Using n - i would give 0 stars in the last row.
Q4. Why is printf("\n") placed after the inner loop and not inside it?
The newline should come once per row, after all the stars of that row are printed. Inside the inner loop, every star would be on its own line.
Q5. What happens if the user enters n = 0 or a negative number?
The outer loop condition i <= n is false at the start, so nothing is printed and the program just ends. There is no error message because the program does not validate input.
Q6. What happens when n = 1?
The outer loop runs once, the inner loop runs n-i+1 = 1 time, so a single star is printed followed by a newline.
Q7. What is the time complexity of this program?
The total stars printed are n + (n-1) + ... + 1 = n(n+1)/2, so the time complexity is O(n^2).
Q8. What would you change to print a right-aligned inverted triangle, with spaces before the stars?
Add another inner loop before the star loop that prints i-1 spaces. This pushes each row to the right, so the stars form a right-aligned inverted triangle.
Frequently asked questions
How many stars are in row i of the inverted triangle?
Row i has n - i + 1 stars. For n = 5, row 1 has 5 stars, row 2 has 4, and so on down to 1 star in row 5.
What is the time complexity of this program?
It is O(n^2), because the total number of stars printed is n + (n-1) + ... + 1 = n(n+1)/2.
How can I print the inverted triangle without using n - i + 1?
Run the outer loop backwards with i from n down to 1 and let the inner loop run j from 1 to i. This prints the same pattern.