Diamond Number Pattern in C

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

What You’ll Learn

How to print a diamond number pattern in C using nested loops. The upper half grows from 1 digit to 9 digits, then the lower half shrinks back to 1.

You’ll also see how to use leading spaces to center-align each row, and how 2 * i - 1 controls the width of the diamond.

⭐ Pattern Output

For rows = 5 (upper half), the pattern looks like this:

Output
    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1
1

Complete C Program

This version prints the upper half for i = 1..rows, then prints the lower half for i = rows-1..1.

c
#include <stdio.h>

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

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

    for (i = rows - 1; i >= 1; i--) {
        for (j = 1; j <= rows - i; j++) {
            printf(" ");
        }
        for (j = 1, k = 1; j <= 2 * i - 1; j++, k++) {
            printf("%d", k);
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Set the row count

int rows = 5; controls the height of the upper half (and thus the full diamond).

Setup
2

Upper half (growing rows)

for (i = 1; i <= rows; i++) builds the top by increasing the row width each time.

Build up
3

Center alignment (spaces)

rows - i leading spaces center each row so the shape looks like a diamond.

Alignment
4

Row width (odd counts)

2 * i - 1 produces widths 1, 3, 5, 7, 9… and k prints 1..(2*i-1).

Width
5

Lower half (shrinking rows)

for (i = rows - 1; i >= 1; i--) mirrors the upper half to complete the diamond.

Mirror
=

Diamond number pattern

Each row prints an odd number of digits, so the work grows roughly like O(n²) with n rows.

2

Variation — User Input Version

Accept the upper-half row count at runtime using scanf():

c
#include <stdio.h>

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

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

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

    for (i = rows - 1; i >= 1; i--) {
        for (j = 1; j <= rows - i; j++) {
            printf(" ");
        }
        for (j = 1, k = 1; j <= 2 * i - 1; j++, k++) {
            printf("%d", k);
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Allow rows input using scanf() (shown above)
  • Add spaces between digits with printf("%d ", k)
  • Print a hollow diamond (only border digits)
  • Replace digits with letters for an alphabet diamond

Avoid

  • Forgetting leading spaces (diamond becomes left-aligned)
  • Using rows <= 0 without validating input
  • Printing an extra newline inside inner loops

Key Takeaways

1

The diamond is made from an upper half (growing) and a lower half (shrinking).

2

Leading spaces (rows - i) keep the output center-aligned.

3

The row width is controlled by 2 * i - 1 (odd numbers).

4

This same idea applies to diamond star patterns too.

❓ Frequently Asked Questions

Because the diamond grows up to the middle row and then shrinks. Two loops make this mirror effect straightforward.
Row i prints 2*i-1 digits: 1, 3, 5, 7, 9…
Increase rows (upper half). For example, rows = 6 will reach width 11 on the middle row.
O(n²) for n rows, since the total printed digits increases quadratically.

Explore More C Number Patterns!

Practice loops with triangles, pyramids, diamonds, and more.

All Number Patterns →
Did you know?

In the example shown (rows = 5), the total digits printed are 1+3+5+7+9+7+5+3+1 = 41. This is why the overall work grows roughly like O(n²) as rows increases.

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