Ascending Number Triangle in C

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print an ascending number triangle in C using nested for loops. Each row starts from 1 and grows by one additional digit on every line.

This is one of the best beginner exercises to understand how the outer loop controls rows and the inner loop prints the correct count for each row.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
12
123
1234
12345
1

Complete C Program

The inner loop runs from 1 to i, so row i prints exactly i numbers.

c
#include <stdio.h>

int main() {
    int rows = 5;
    int i, j;

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

    return 0;
}

🧠 How It Works

1

Set the number of rows

rows = 5 controls the triangle height.

Setup
2

Outer loop (rows)

i goes from 1 to rows. Each iteration prints one row.

Row control
3

Inner loop (print 1..i)

j runs from 1 to i, so row i prints 1..i.

Grow each row
4

New line

printf("\n") moves to the next row.

Line break
=

Ascending triangle

Total prints are 1+2+…+n = n(n+1)/2, so time complexity is O(n²).

2

Variation — User Input Version

Accept the number of rows from the user using scanf():

c
#include <stdio.h>

int main() {
    int rows;
    int i, j;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits with printf("%d ", j)
  • Print a descending-per-row version by counting j down from i
  • Right-align the triangle by printing leading spaces before each row
  • Replace digits with letters to build alphabet triangles

Avoid

  • Forgetting printf("\n") after each row
  • Printing extra spaces unless you want spaced formatting
  • Using non-positive row counts without validation

Key Takeaways

1

Row i prints numbers from 1 to i.

2

The inner loop bound j <= i is what makes the triangle grow by one digit each row.

3

Total prints are n(n+1)/2, so runtime is O(n²).

4

This is the ascending counterpart to the descending number triangle.

❓ Frequently Asked Questions

Because the inner loop starts at j = 1 each time. Only the end value (j <= i) changes per row.
Use a separate counter variable that increments across rows instead of printing j.
O(n²) because total prints are 1+2+…+n = n(n+1)/2.

Try More Number Triangles

Once you know the basic ascending triangle, you can create dozens of variants by changing where the inner loop starts and ends.

All Number Patterns →
Did you know?

This is the same growth rule as the classic star triangle: the inner loop runs exactly i times on row i. Only the printed symbol changes.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful