Right Triangle Star Pattern: flowchart, algorithm and program
This program reads n and prints a right-angled triangle of stars with n rows, where row i has exactly i stars. It uses two loops: the outer loop picks the row, and the inner loop prints the stars for that row.
▶ 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 <= i, go to step 7; otherwise go to step 9.
- Step 7: Print "*".
- Step 8: Set j = j + 1. Go to step 6.
- Step 9: Print newline.
- Step 10: Set i = i + 1. Go to step 4.
- Step 11: Stop.
Pseudocode
BEGIN
READ n
i ← 1
WHILE i <= n DO
j ← 1
WHILE j <= i DO
PRINT "*"
j ← j + 1
END WHILE
PRINT newline
i ← i + 1
END WHILE
END
Program in C, Python and Java
Right 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 <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}Right Triangle Star Pattern program in Python
n = int(input("Enter n: "))
for i in range(1, n + 1):
for j in range(1, i + 1):
print("*", end="")
print()Right 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 <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Sample input: n = 5
Output: *
**
***
****
*****
How it works
- scanf reads n, the number of rows in the triangle.
- The outer loop runs i from 1 to n. Each value of i is one row, and it is also the number of stars that row needs.
- For each row, the inner loop runs j from 1 to i and prints one * on every pass. So row 1 gets 1 star, row 2 gets 2 stars, and so on.
- When the inner loop finishes, printf("\n") moves the cursor to the next line so the next row starts fresh.
- Because the inner loop's limit is i and not n, the row length grows by one each time, and that is what makes the triangle shape.
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 <= i? | 5 | 1 | 1 | |
| 7 | 9 | Print "*" | 5 | 1 | 1 | * |
| 8 | 8 | j = j + 1 | 5 | 1 | 2 | |
| 9 | 8 | j <= i? | 5 | 1 | 2 | |
| 10 | 11 | Print newline | 5 | 1 | 2 | |
| 11 | 7 | i = i + 1 | 5 | 2 | 2 | |
| 12 | 7 | i <= n? | 5 | 2 | 2 | |
| 13 | 8 | j = 1 | 5 | 2 | 1 | |
| 14 | 8 | j <= i? | 5 | 2 | 1 | |
| 15 | 9 | Print "*" | 5 | 2 | 1 | * |
| 16 | 8 | j = j + 1 | 5 | 2 | 2 | |
| 17 | 8 | j <= i? | 5 | 2 | 2 | |
| 18 | 9 | Print "*" | 5 | 2 | 2 | * |
| 19 | 8 | j = j + 1 | 5 | 2 | 3 | |
| 20 | 8 | j <= i? | 5 | 2 | 3 |
Time and space complexity
Time: O(n²) · Space: O(1). The inner loop runs i times for each i from 1 to n, giving n(n+1)/2 prints, and only a few variables are used.
Common mistakes
- Using j <= n in the inner loop. That prints n stars in every row and gives a square instead of a triangle.
- Forgetting printf("\n") after the inner loop, so all the stars end up on one long line.
- Starting i or j at 0 with the same <= condition, which prints one extra star per row (and one extra row).
Viva questions
Q1. What does this program print, and what is the role of the outer loop?
It prints a right-angled triangle of stars where row i has i stars. The outer loop with i from 1 to n controls the rows, running once per line of the pattern.
Q2. Why does the inner loop run from j = 1 to j <= i and not up to n?
The inner loop's bound decides how many stars go on a row. Because it stops at i, row 1 gets 1 star, row 2 gets 2, and so on. If it ran up to n, every row would have n stars and the output would be a square.
Q3. What is the purpose of printf("\n") on line 11, and why is it placed outside the inner loop?
It moves the cursor to the next line after a row is finished. It sits after the inner loop but inside the outer loop, so it runs once per row. Inside the inner loop, every star would be on its own line.
Q4. Why does i start at 1 and not 0?
Row i must have i stars, and the first row should have 1 star. Starting at 0 would produce an empty first row and shift the pattern by one.
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. For n = 1 it prints a single star.
Q6. What is the time complexity of this program?
The total number of stars printed is 1 + 2 + ... + n = n(n+1)/2, so the time complexity is O(n^2). The extra space used is O(1).
Q7. How would you change the program to print the triangle upside down (n stars first, then n-1, down to 1)?
Make the outer loop count down, for (i = n; i >= 1; i--), and keep the inner loop j <= i. Alternatively, keep the outer loop as it is and make the inner loop run while j <= n - i + 1.
Q8. How would you change the program to print the triangle right-aligned, with the stars on the right side?
Before the stars on each row, add another inner loop that prints n - i spaces. The spaces push the i stars to the right, so the stars line up against the right edge.
Frequently asked questions
How does the inner loop know how many stars to print?
Its condition is j <= i, so it repeats exactly i times. When the outer loop moves to the next row, i goes up by 1 and the inner loop prints one more star.
How many stars are printed in total for n = 5?
1 + 2 + 3 + 4 + 5 = 15 stars. In general it is n(n+1)/2, and the inner printf runs that many times.
How can I print the triangle upside down?
Run the outer loop from i = n down to 1 and keep the inner loop j <= i. Row 1 then has n stars and the last row has 1.