Ascending Number Triangle in C

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:
1
12
123
1234
12345Complete C Program
The inner loop runs from 1 to i, so row i prints exactly i numbers.
#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
Set the number of rows
rows = 5 controls the triangle height.
Outer loop (rows)
i goes from 1 to rows. Each iteration prints one row.
Inner loop (print 1..i)
j runs from 1 to i, so row i prints 1..i.
New line
printf("\n") moves to the next row.
Ascending triangle
Total prints are 1+2+…+n = n(n+1)/2, so time complexity is O(n²).
Variation — User Input Version
Accept the number of rows from the user using scanf():
#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
jdown fromi - 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
Row i prints numbers from 1 to i.
The inner loop bound j <= i is what makes the triangle grow by one digit each row.
Total prints are n(n+1)/2, so runtime is O(n²).
This is the ascending counterpart to the descending number triangle.
❓ Frequently Asked Questions
j = 1 each time. Only the end value (j <= i) changes per row.j.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.
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.
12 people found this page helpful
