C Increasing Number Triangle Pattern (Right-Aligned)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A right-aligned ascending triangle prints 1 through i on each row, with leading spaces so shorter rows sit against the right edge. %2d keeps each number two columns wide.

Remember
Rule: for i from 1 to rows
        print (rows - i) spaces
        print k from 1 to i with %2d

     1
    1 2
   1 2 3
  1 2 3 4
 1 2 3 4 5     ← rows = 5

Follows the hollow square in Program 42; next is Program 44.

How to Solve It

Start with fixed rows = 5, then generalize with scanf — optionally drop the space loop for a left-aligned contrast.

MethodIdeaBest for
Spaces then numbersIndent while j > i; print 1..i with %2dLearning, interviews, exams
Left-alignedSame numbers; skip the space loopSeeing what indentation adds

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from rows down to i + 1:
        print one space
    for k from 1 to i:
        print k in width 2
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Leading spacesfor (j = rows; j > i; j--) printf(" ");
Print 1..ifor (k = 1; k <= i; k++) printf("%2d", k);
End the rowprintf("\n");
Space countrows - i

Printing Numbers vs Starting a New Line

APIEffectUse for
printf(" ") / printf("%2d", k)Stays on the same lineEach space and each number
printf("\n")Ends the current lineAfter both inner loops

Print spaces and numbers without a newline, then end the row once.

Live Preview

Change the row count and the right-aligned triangle updates instantly — each number uses a width-2 field like %2d.

Whole numbers from 1 to 9 so every value stays within %2d. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 numbers
     1
    1 2
   1 2 3
  1 2 3 4
 1 2 3 4 5

Worked Walkthrough — rows = 4

Trace each outer-loop value of i, how many spaces print, and which numbers follow.

iSpacesNumbersPrinted row
1311
221, 21 2
311, 2, 31 2 3
401, 2, 3, 41 2 3 4

Number prints for n rows = n(n + 1)/2 (here 10). Space cells add more work of the same order — why time is O(n²).

C Programs

Three complete programs: fixed rows, scanf input, and a left-aligned contrast. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded row count — spaces while j > i, then %2d for 1..i.

C
#include <stdio.h>

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

    for (i = 1; i <= rows; i++)
    {
        for (j = rows; j > i; j--)
            printf(" ");

        for (k = 1; k <= i; k++)
            printf("%2d", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Outer loop grows i. Each pass adds one more number and drops one leading space.

2. Spaces then digits. The j loop prints rows - i spaces; the k loop prints 1..i with %2d.

3. End the line. printf("\n") runs only after both inner loops finish.

When i = 1 you get four spaces then 1; when i = 5 you get 1 2 3 4 5 with no indent.

Example 2 — User Input Version

Read rows with scanf and reject non-positive values. Same space-and-number core.

C
#include <stdio.h>

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

    printf("Enter number of rows: ");
    if (scanf("%d", &rows) != 1 || rows <= 0)
    {
        printf("Please enter a positive integer.\n");
        return 1;
    }

    for (i = 1; i <= rows; i++)
    {
        for (j = rows; j > i; j--)
            printf(" ");

        for (k = 1; k <= i; k++)
            printf("%2d", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject bad input with a clear message instead of looping on garbage.

2. Same core. Only the hard-coded 5 becomes the user value in both loops.

3. Safer input tip. You can also clamp demos to single-digit values:

Safer input
if (scanf("%d", &rows) != 1 || rows < 1 || rows > 9)
{
    printf("Enter a whole number from 1 to 9.\n");
    return 1;
}

Example 3 — Left-Aligned Triangle

Same ascending sequence without leading spaces — numbers start flush left.

C
#include <stdio.h>

int main(void)
{
    int rows = 5;
    int i, k;

    for (i = 1; i <= rows; i++)
    {
        for (k = 1; k <= i; k++)
            printf("%2d", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Same numbers. The k loop and %2d formatting stay identical to Example 1.

2. Drop the indent. Removing the space loop leaves every row flush left — useful for comparing alignment.

3. Restore spaces next. Once the digit sequence is clear, put the j loop back for the right-aligned look.

Edge Cases & Pitfalls

Check these before calling the solution done.

no spaces

Left-aligned by accident

Skipping the j loop removes the indent. Keep for (j = rows; j > i; j--) for right alignment.

no %2d

Crowded columns

Plain printf("%d", k) makes two-digit values crowd earlier columns. Keep %2d (or wider for larger rows).

\n inside

Broken rows

If printf("\n") sits inside either inner loop, each token lands on its own line. Call the newline only after both loops.

rows > 9

%2d overflow

Values from 10 upward need three columns. Clamp demos to 1–9, or switch to %3d.

rows = 1

Smallest triangle

Output is a single width-2 1 with no leading spaces. A good sanity check.

scanf

Check the return value

If scanf fails, rows may be uninitialized — always test scanf(...) == 1.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(rows²)O(1)
Left-aligned (Example 3)O(rows²)O(1)

Number prints = n(n + 1)/2; space cells are also triangular. For rows = 5 that is 15 numbers (plus 10 indent spaces).

Key Takeaways

  • Rule: print rows - i spaces, then 1..i with %2d.
  • Two inner loops: indent first, digits second — order matters for the right edge.
  • Break the row: call printf("\n") only after both inner loops.
  • Complexity: O(n²) time from spaces plus numbers; O(1) extra space.

One line: for each i, print rows - i spaces, then %2d for 1..i, then printf("\n").

Frequently Asked Questions

A right-aligned ascending triangle: row 1 prints 1, row 2 prints 1 2, row 3 prints 1 2 3, and so on until 1 2 3 4 5.
Before printing numbers, the program prints spaces while j > i. Smaller rows get more spaces, pushing digits to the right edge.
The format specifier reserves 2 columns per number (right-aligned), keeping columns stable when values become two digits.
Remove the space loop that prints leading spaces. Then print numbers 1 to i directly — see Example 3.
Program 42 prints a hollow square grid with border conditions. Program 43 prints an ascending number triangle with indentation spaces.
Use a rows variable and loop i from 1 to rows — see Example 2.
O(n²) for n rows because total number prints are 1 + 2 + ... + n = n(n+1)/2 (plus O(n²) space cells).
Check scanf's return value and reject non-positive row counts so the outer loop has a valid range.

Did you know?

Each row prints numbers 1 to i. A space loop runs while j > i before the number loop; %2d keeps columns aligned in the output.

Next: Number Diamond Pattern

Continue with a centered diamond that grows and shrinks ascending digit rows.

Program 44 tutorial →

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