Left-Shifted Sequential Number Triangle 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. Each next row starts with a number that is one higher than the previous row, while values inside a row increase sequentially.

This is a great exercise to practice how the outer loop controls rows and the inner loop controls how many numbers appear per row.

⭐ Pattern Output

For rows = 5 and start_num = 11, the pattern looks like this:

Output
11
12 13
13 14 15
14 15 16 17
15 16 17 18 19
1

Complete C Program

For each row i, we set num = start_num + i (so each row starts one number later), then print i+1 values while incrementing num.

c
#include <stdio.h>

int main() {
    int rows = 5;
    int start_num = 11;

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

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

        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Set rows and starting number

We define how many lines to print and the first value of row 1: rows = 5, start_num = 11.

Setup
2

Outer loop controls the rows

for (i = 0; i < rows; i++) prints rows from 0 to 4 (total 5 rows).

Row control
3

Start each row one number later

We compute the row start as num = start_num + i. That gives 11, 12, 13, 14, 15 for rows 1..5.

Left-shift
4

Inner loop prints i+1 values

for (j = 0; j <= i; j++) prints 1 value on row 1, 2 values on row 2, etc. After printing one value, we do num++.

Number printing
=

Left-shifted sequential triangle

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 both the row count and the starting number using scanf():

c
#include <stdio.h>

int main() {
    int rows, start_num;

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

    printf("Enter the starting number: ");
    scanf("%d", &start_num);

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Change start_num to generate a new sequence
  • Print without trailing spaces by handling the last print in each row
  • Use printf("%02d ", num) to keep equal-width columns
  • Right-align the triangle by printing leading spaces per row
  • Swap numbers for characters to build alphabet variations

Avoid

  • Forgetting printf("\n") after each row
  • Letting rows be zero/negative without validating input
  • Resetting num incorrectly (the row must start at start_num + i)
  • Mixing row and column conditions (outer loop for rows, inner loop for columns)

Key Takeaways

1

The outer loop chooses the row and its length (i+1 values).

2

Row starts are computed using start_num + i to create the left-shift.

3

The inner loop prints sequential values by incrementing num.

4

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

❓ Frequently Asked Questions

Because we set num = start_num + i, and i increases by 1 for each next row.
Yes. Print a space only when j < i, or print the last value separately without a trailing space.
Set start_num = 1. The rest of the code remains the same.
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 1+2+…+n values in total. That sum is n(n+1)/2, which is why many pattern programs 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