Printing a triangle of stars may look like a puzzle, but it is one of the best exercises for mastering nested loops. Every pattern problem is solved in the same way: find the rule that connects the row number to what is printed in that row.

Rows and Columns

A pattern is printed row by row. The outer loop controls the rows. The inner loop controls what is printed within one row. After the inner loop ends, a newline moves the output to the next row.

Finding the Rule

The method has three steps.

  1. Write the row number beside each row of the required pattern.
  2. Count the symbols in each row and express the count in terms of the row number.
  3. Use that expression as the limit of the inner loop.

For a right-angled triangle of stars, row 1 has one star, row 2 has two and row i has i stars. The inner loop therefore runs from 1 to i.

Definition: Nested iteration. The use of one loop inside another, in which the inner loop is carried out completely for each pass of the outer loop.

Common Pattern Rules

Table 2.6 lists the inner-loop rule for some common patterns of n rows.

Table 2.6: Inner-loop limits for common patterns of n rows

Pattern Symbols in row i Inner loop
Rectangle of n columns n j from 1 to n
Right-angled triangle i j from 1 to i
Inverted triangle n − i + 1 j from 1 to n − i + 1
Number triangle (1, 12, 123) i numbers j from 1 to i, print j
Repeated-row triangle (1, 22, 333) i numbers j from 1 to i, print i

Symbol Patterns and Number Patterns

In a symbol pattern the same character is printed each time. In a number pattern the value printed changes. Two choices are common: printing the inner counter j gives rows such as 1, 12, 123, while printing the outer counter i gives rows such as 1, 22, 333. The loops are identical; only the printed value differs.

Patterns with Leading Spaces

A pattern such as a pyramid needs spaces before the symbols. Each row then uses two inner loops stacked one after the other: the first prints the spaces, and the second prints the symbols. For a pyramid of n rows, row i has n − i spaces followed by 2i − 1 stars.

Example 2.6: A number triangle

Problem: Display the following pattern for n = 4: row 1 shows 1, row 2 shows 1 2, row 3 shows 1 2 3 and row 4 shows 1 2 3 4.

Solution:

  1. Row i contains the numbers 1 to i, so the inner loop runs from 1 to i.
  2. The value printed is the inner counter j.
  3. A newline is printed after each row.
#include <stdio.h>

int main(void)
{
    int i, j, n = 4;

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%d ", j);
        }
        printf("\n");
    }
    return 0;
}

Output:

1
1 2
1 2 3
1 2 3 4

Result: The inner loop limit i makes each row one number longer than the row before it. Replacing printf("%d ", j) with printf("%d ", i) would give the rows 1, 2 2, 3 3 3 and 4 4 4 4.

Common mistakes

  • Placing the newline inside the inner loop, which puts every symbol on its own line.
  • Using a fixed limit for the inner loop when it should depend on the row number.
  • Confusing the row counter with the column counter when choosing the value to print.

Key takeaways

  • The outer loop controls rows and the inner loop controls the contents of a row.
  • The key step is expressing the number of symbols in a row in terms of the row number.
  • Printing j or i inside the same loops produces different number patterns.
  • Patterns with leading spaces use two stacked inner loops per row.

Practice questions

  1. In a right-angled triangle pattern, how many stars does row i contain?
  2. How many stars does row 2 of an inverted triangle of 5 rows contain?
  3. Where must the newline statement be placed in a pattern program?

Answers to the practice questions

  1. i stars.
  2. n − i + 1 = 5 − 2 + 1 = 4 stars.
  3. Inside the outer loop but after the inner loop, so that it runs once per row.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit II