Sunday, August 31, 2025

Printing Number Triangle (Half Pyramid) | Number Patterns in C

This tutorial will teach you how to create a half pyramid number pattern in C, a fundamental exercise for beginners to master nested loops and pattern-building logic. By the end, you'll be able to create this classic pattern and understand the core concepts behind it.

Final Output

The final output is a number triangle where each row prints a sequence of numbers up to the row number itself. For a user input of 5, the output will look like this:

Enter the number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Deconstructing the Pattern: The Logic

Creating a pattern requires breaking it down into rows and columns1. For this pattern, the logic is based on controlling two loops: an outer loop for the rows and an inner loop for the columns.

Problem Statement: The program should take an integer input n from the number of rows from the user and print a number triangle pattern where each row contains numbers from 1 up to the current row number.

Pattern Analysis & Logic:

  • Identifying the Rows: The pattern has n rows, so our outer loop will run from i = 1 to n2. This loop controls the row number.

  • Identifying the Columns: The number of characters in each row is equal to the row number3. This means our inner loop will run from

    j = 1 to i4. This loop controls the numbers printed on each row.

  • Printing the Numbers: Inside the inner loop, we will print the value of j. Since the inner loop's counter j starts at 1 and goes up to i, this naturally prints the sequence 1, 1 2, 1 2 3, and so on.

Here is the step-by-step breakdown of the algorithm:

  1. Start the program.

  2. Declare an integer variable n for the number of rows.

  3. Prompt the user to enter the number of rows.

  4. Read the user's input and store it in n.

  5. Create an outer for loop that iterates from i = 1 to n (for the rows).

  6. Inside the outer loop, create an inner for loop that iterates from j = 1 to i (for the columns).

  7. Inside the inner loop, print the value of j followed by a space.

  8. After the inner loop finishes (meaning a full row has been printed), use

    printf("\n"); to move to the next line5.

  9. Repeat the process until the outer loop completes.


The Code Implementation

Here is the complete C program, with inline comments to explain each key line of code6.

C
#include <stdio.h>

int main() {
    // Declare variables for rows and user input
    int i, j, n;

    // Prompt the user for the number of rows
    printf("Enter the number of rows: ");
    scanf("%d", &n);

    // Outer loop for the rows
    for (i = 1; i <= n; i++) {
        // Inner loop for the columns
        for (j = 1; j <= i; j++) {
            // Print the number and a space
            printf("%d ", j);
        }
        // Move to the next line after each row is complete
        printf("\n");
    }

    return 0;
}

Explanation:

  • int i, j, n;: We declare three integer variables: n to store the user input, i for the outer loop (rows), and j for the inner loop (columns)7.

  • for (i = 1; i <= n; i++): This outer loop iterates from 1 up to the number of rows entered by the user. In each iteration, it represents a new row8.

  • for (j = 1; j <= i; j++): This inner loop runs for each row. The condition

    j <= i ensures that the number of elements printed in each row is equal to the current row number i9.

  • printf("%d ", j);: This statement prints the value of j followed by a space. Since j increments in the inner loop, it prints 1, then 1 2, then 1 2 3, and so on.

  • printf("\n");: After the inner loop completes its execution for a given row, this statement moves the cursor to the next line, preparing for the next row of the pattern10.


Sample Output

Here is an example of what you'll see when you compile and run the program with a sample input.

Enter the number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Output Analysis:

  • Row 1 (i=1): The inner loop runs from j=1 to 1, printing 1. A newline is then printed.

  • Row 2 (i=2): The inner loop runs from j=1 to 2, printing 1 2. A newline is then printed.

  • Row 3 (i=3): The inner loop runs from j=1 to 3, printing 1 2 3. A newline is then printed.

  • The pattern continues this way until i reaches 5.


    Dry-Run Table

    A dry-run table is an excellent way to show how the variables change during each iteration of the loop.

    Dry-Run Table of Printing Number Triangle (Half Pyramid) | Number Patterns in C




Variations and Enhancements

  • Change the Logic: Modify the printf statement to print i instead of j11. This will produce a pattern where each row prints the row number repeatedly (e.g.,

    1, 2 2, 3 3 3).

  • Modify the Character: Suggest how to convert the pattern to a star, hollow, mirrored, or alphabet version12.

  • Challenge: Try to modify the code to create an inverted version of this pattern13.


Common Mistakes and Troubleshooting

  • Incorrect Loop Conditions: A small typo in the loop conditions, such as i < n, can completely change the pattern14. Ensure your loops run for the correct range.

  • Missing Newline: Forgetting the printf("\n"); statement will cause all the numbers to print on a single line, making a horizontal pattern instead of a triangle15.


Complexity Analysis

  • Time Complexity: The time complexity is O(n^2) because of the nested loops. The inner loop's execution depends on the outer loop, so the total number of operations is roughly proportional to the square of

    n16.

  • Space Complexity: The space complexity is O(1) as the program uses a constant amount of memory for variables, regardless of the number of rows entered by the user17.


Conclusion

You have successfully learned how to build a number triangle pattern using nested loops. This exercise reinforces the critical concepts of loop control and pattern analysis, which are the building blocks for more complex programming challenges.

....till next post, bye-bye & take care!

No comments:

Post a Comment