Odd-Length Consecutive Number Pyramid in C

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Step Increment

What You’ll Learn

How to print an odd-length consecutive number pyramid in C. Each row prints consecutive digits starting from 1, but the row length increases by 2 each time: 1, 3, 5, 7, 9…

The key idea is using i += 2 in the outer loop to generate only odd row lengths.

⭐ Pattern Output

For a maximum width of 9, the pattern looks like this:

Output
1
123
12345
1234567
123456789
1

Complete C Program

The outer loop picks the row length (odd numbers only). The inner loop prints 1..i in each row.

c
#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 9; i += 2) {
        for (j = 1; j <= i; ++j) {
            printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Use odd row lengths

for (i = 1; i <= 9; i += 2) produces 1, 3, 5, 7, 9.

Step increment
2

Print 1..i each row

for (j = 1; j <= i; ++j) prints consecutive digits starting from 1.

Inner loop
3

Move to next line

printf("\\n") ends the current row.

Row break
=

Odd-length pyramid

Total prints are 1+3+5+…+n, which grows on the order of , so time complexity is O(n²).

2

Variation — User Input Version

Let the user choose the maximum width. If the user enters an even number, you can still print odd rows up to it (or subtract 1).

c
#include <stdio.h>

int main() {
    int max;
    int i, j;

    printf("Enter the maximum width (odd number recommended): ");
    scanf("%d", &max);

    for (i = 1; i <= max; i += 2) {
        for (j = 1; j <= i; ++j) {
            printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print even-length rows by starting at 2 and using i += 2
  • Add spaces with printf("%d ", j)
  • Right-align by printing leading spaces before each row
  • Change the start digit to shift the pyramid
  • Replace digits with letters for an alphabet pyramid

Avoid

  • Using i++ (it breaks the odd-length requirement)
  • Forgetting the newline between rows
  • Printing without spacing when multi-digit output is expected
  • Assuming max is odd if you accept user input
  • Mixing row/column roles in your loops

Key Takeaways

1

Use i += 2 to generate odd row lengths (1, 3, 5…).

2

Each row prints consecutive digits from 1 to i.

3

The same structure works for even-length or spaced variants.

4

Total operations grow on the order of .

❓ Frequently Asked Questions

It ensures the row length stays odd: 1, 3, 5, 7, 9…
Start the outer loop at 2 and still add 2 each time: for (i = 2; i <= max; i += 2).
O(n²) with respect to the maximum width n, because you print 1+3+5+…+n values.
Because the inner loop prints j from 1 up to the row length i.

Explore More C Number Patterns!

Practice with pyramids and triangles that change row length rules.

All Number Patterns →
Did you know?

The sum of the first \(k\) odd numbers is \(k^2\). That’s why printing 1, 3, 5, 7, 9 values grows like a square as the pyramid widens.

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