Left-Shifted Sequential Number Triangle (Start from 0) in C

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

What You’ll Learn

How to print a left-shifted sequential number triangle in C that starts from 0. Each row begins with its row index (0, 1, 2, ...), then continues sequentially across the row.

This is a nice example of using nested loops and resetting a row variable (num) at the start of each row.

⭐ Pattern Output

For rows = 6, the pattern looks like this:

Output
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10
1

Complete C Program

We start each row with num = i (the row index). Then we print i+1 values while incrementing num.

c
#include <stdio.h>

int main() {
    int rows = 6;

    for (int i = 0; i < rows; i++) {
        int num = i;

        for (int j = 0; j <= i; j++) {
            printf("%d ", num);
            num++;
        }

        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Choose the number of rows

rows = 6 prints 6 lines (0..5 as row indices).

Setup
2

Outer loop controls the row index

i goes from 0 to 5 and determines the row length (i+1 values).

Row control
3

Start each row at i

We set num = i, so the first value in each row equals the row index.

Row start
4

Inner loop prints sequential values

The inner loop prints i+1 values, incrementing num++ after each print.

Number printing
=

Left-shifted sequential triangle (from 0)

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

2

Variation — User Input Version

Accept the number of rows using scanf():

c
#include <stdio.h>

int main() {
    int rows;

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

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Use an offset like num = i + 10 to start from 10
  • Print without trailing spaces by handling the last value in each row
  • Use printf("%02d ", num) for equal-width columns
  • Create a right-aligned version by adding leading spaces
  • Swap numbers for letters to build alphabet variations

Avoid

  • Forgetting printf("\n") after each row
  • Not resetting num at the beginning of each row
  • Mixing row and column logic (outer loop rows, inner loop columns)
  • Using negative row counts without input validation

Key Takeaways

1

The outer loop picks the row index i and row length (i+1 values).

2

Setting num = i makes each row start at its row index.

3

The inner loop prints sequential values by incrementing num.

4

Time complexity is O(n²) for n rows.

❓ Frequently Asked Questions

On the first row, i = 0, so the inner loop runs once and prints num = 0.
Use int num = i + 10; at the start of each row.
Yes. Print a space only when j < i, or print the last value separately without a trailing space.
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?

For n rows, you print n(n+1)/2 values in total. That sum is why many triangle patterns naturally have O(n²) runtime.

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