Right-Aligned Continuous Number Pattern in C

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

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:

Output
             1
          2  3
       4  5  6
    7  8  9 10
11 12 13 14 15
1

Complete C Program

We print rows - i groups of spaces to align right, then print i numbers using a continuous counter.

c
#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

1

Initialize rows and counter

rows = 5 sets the height, and number = 1 keeps counting across all rows.

Setup
2

Outer loop sets the row length

For row i, we print exactly i numbers.

Row control
3

Print leading spaces

We print rows - i groups of " " to shift the row to the right.

Alignment
4

Print numbers in a fixed width

Using %3d keeps columns aligned when numbers become two digits, and number++ continues the sequence.

Number printing
=

Right-aligned continuous triangle

Total printed values are 1+2+…+n = n(n+1)/2, so runtime is O(n²) for n rows.

2

Variation — User Input Version

Accept the row count using scanf():

c
#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 number to start from a different value
  • Use %4d if you plan to print larger sequences
  • Convert it into a centered pyramid by adjusting spacing
  • Replace numbers with letters using %3c

Avoid

  • Using %d and expecting alignment for 2-digit numbers
  • Forgetting printf("\n") after each row
  • Resetting number inside the outer loop (breaks continuity)
  • Printing too few spaces (misaligns rows)

Key Takeaways

1

A single counter variable keeps numbers continuous across rows.

2

Leading spaces create the right-aligned triangle.

3

%3d keeps columns aligned for multi-digit numbers.

4

Runtime is O(n²) for n rows.

❓ Frequently Asked Questions

Those spaces shift the row to the right so all rows line up on the right edge.
If you use %d, 1-digit and 2-digit numbers take different widths. Using %3d makes every number occupy the same width.
Remove the loop that prints printf(" ") before the numbers.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Explore More C Number Patterns!

Keep practicing nested loops with more number pattern variations—from triangles to pyramids.

All Number Patterns →
Did you know?

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.

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