Diagonal Increment Number Triangle in C

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Variable Increments

What You’ll Learn

How to print a diagonal increment number triangle in C. Each row starts with its row number, then continues by adding a decreasing increment \((rows - j)\).

This pattern is a great way to practice nested loops and custom increment rules inside the inner loop.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

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

Complete C Program

We set currentNumber to the row start, print it, then add rows - j each step to get the next value.

c
#include <stdio.h>

int main() {
    int rows = 5;
    int start = 1;
    int i, j;

    for (i = 1; i <= rows; ++i) {
        int currentNumber = start;

        for (j = 1; j <= i; ++j) {
            printf("%d ", currentNumber);
            currentNumber += rows - j;
        }

        printf("\n");
        ++start;
    }

    return 0;
}

🧠 How It Works

1

Pick the row count

rows = 5 sets the triangle height and drives the increment formula.

Setup
2

Start value per row

We use start to track the first number of each row: row 1 starts at 1, row 2 starts at 2, etc.

Row start
3

Decreasing increment rule

After printing a value, we do currentNumber += rows - j. When j increases, \((rows - j)\) decreases, so the increments shrink as you move right.

Increment
=

Diagonal-like progression

Total prints are 1+2+…+n = n(n+1)/2, so time complexity is O(n²).

2

Variation — User Input Version

Make it dynamic by reading the number of rows from the user:

c
#include <stdio.h>

int main() {
    int rows;
    int start = 1;
    int i, j;

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

    for (i = 1; i <= rows; ++i) {
        int currentNumber = start;

        for (j = 1; j <= i; ++j) {
            printf("%d ", currentNumber);
            currentNumber += rows - j;
        }

        printf("\n");
        ++start;
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Make increments increase by using currentNumber += j
  • Print aligned columns using a fixed width format like printf("%3d", currentNumber)
  • Change the starting value to shift the whole triangle
  • Create a right-aligned version by printing leading spaces
  • Convert to characters for a diagonal alphabet pattern

Avoid

  • Resetting start inside the outer loop
  • Using the wrong increment formula (it changes the pattern)
  • Forgetting the newline between rows
  • Allowing invalid rows without basic validation
  • Printing without spacing if you want multi-digit readability

Key Takeaways

1

Each row starts with its row number (tracked via start).

2

Next values are generated using the decreasing increment \((rows - j)\).

3

Nested loops define the triangle shape; the increment rule defines the diagonal effect.

4

Total work is O(n²) for n rows.

❓ Frequently Asked Questions

Because the formula uses rows - j. As j grows (moving right), the value of rows - j becomes smaller.
Yes. Replace currentNumber += rows - j with currentNumber += j.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.
It lets each row compute its own sequence starting from the row’s first value, without affecting other rows.

Explore More C Number Patterns!

Keep practicing with patterns that use custom rules to generate each next value.

All Number Patterns →
Did you know?

Changing only the inner-loop update rule can generate dozens of unique number patterns—the triangle shape stays the same, but the sequence behavior changes.

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