Right-Aligned Continuous Number Pattern in C

What You’ll Learn
How to print a right-aligned continuous number triangle in C. The numbers increase continuously across rows (1, 2, 3, ...) and the triangle is right-aligned by printing leading spaces.
We also use %3d so single-digit and double-digit numbers stay aligned in neat columns.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15Complete C Program
We print rows - i groups of spaces to align right, then print i numbers using a continuous counter.
#include <stdio.h>
int main() {
int rows = 5;
int number = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
printf("%3d", number);
number++;
}
printf("\n");
}
return 0;
}🧠 How It Works
Initialize rows and counter
rows = 5 sets the height, and number = 1 keeps counting across all rows.
Outer loop sets the row length
For row i, we print exactly i numbers.
Print leading spaces
We print rows - i groups of " " to shift the row to the right.
Print numbers in a fixed width
Using %3d keeps columns aligned when numbers become two digits, and number++ continues the sequence.
Right-aligned continuous triangle
Total printed values are 1+2+…+n = n(n+1)/2, so runtime is O(n²) for n rows.
Variation — User Input Version
Accept the row count using scanf():
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
int number = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
printf("%3d", number);
number++;
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Remove the space loop to make it left-aligned
- Change
numberto start from a different value - Use
%4dif you plan to print larger sequences - Convert it into a centered pyramid by adjusting spacing
- Replace numbers with letters using
%3c
Avoid
- Using
%dand expecting alignment for 2-digit numbers - Forgetting
printf("\n")after each row - Resetting
numberinside the outer loop (breaks continuity) - Printing too few spaces (misaligns rows)
Key Takeaways
A single counter variable keeps numbers continuous across rows.
Leading spaces create the right-aligned triangle.
%3d keeps columns aligned for multi-digit numbers.
Runtime is O(n²) for n rows.
❓ Frequently Asked Questions
%d, 1-digit and 2-digit numbers take different widths. Using %3d makes every number occupy the same width.printf(" ") before the numbers.Explore More C Number Patterns!
Keep practicing nested loops with more number pattern variations—from triangles to pyramids.
Formatting output is just as important as generating it. Using fixed-width formats (like %3d) helps your patterns look correct even when values grow beyond 9.
12 people found this page helpful
