Diamond Repeating Numbers with Asterisks in C

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

What You’ll Learn

How to print a diamond repeating number pattern in C where each row repeats the same digit, separated by *.

The pattern grows from 1 to rows and then mirrors back down to 1.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
1

Complete C Program

Print an increasing half (1..rows), then a decreasing half (rows-1..1). Add * between repeated numbers.

c
#include <stdio.h>

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

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

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

    return 0;
}

🧠 How It Works

1

Print the upper half

The first outer loop runs from 1 to rows and prints 1 row per value.

Upper half
2

Repeat the number with separators

The inner loop repeats the row value, and prints * between repeats (not after the last one).

Separator
3

Print the lower half

The second outer loop runs from rows-1 down to 1 to mirror the upper half.

Lower half
=

Diamond mirror effect

Upper half grows, lower half shrinks, producing a symmetric pattern.

2

Variation — User Input Version

Read rows and generate the same diamond pattern.

c
#include <stdio.h>

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

    int i, j;
    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d", i);
            if (j != i) {
                printf("*");
            }
        }
        printf("\n");
    }

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Replace * with a different separator like - or |
  • Remove the separator to get 1, 22, 333, …
  • Add spaces around separators for readability
  • Print letters instead of numbers for an alphabet diamond
  • Increase rows to create larger diamonds

Avoid

  • Printing a trailing separator after the last number
  • Forgetting the lower half loop (would not form a diamond)
  • Not validating user input (rows <= 0)
  • Mixing row value and repeat count (keep them consistent)

Key Takeaways

1

Two outer loops create the diamond: up then down.

2

The inner loop repeats the same value on each row.

3

A simple condition prevents printing a trailing *.

4

Mirroring patterns are common in diamond and hourglass shapes.

❓ Frequently Asked Questions

The pattern increases from 1 to rows and then decreases back to 1. Each row repeats the same digit separated by *, producing a symmetric diamond-like output.
One half builds the increasing part, and the other half mirrors it by decreasing, which creates the diamond symmetry.
Yes. Replace printf("*") with any character like printf("-").
O(n²) for n rows due to nested loops across both halves.

Explore More C Number Patterns!

Mirror patterns like this are a stepping stone to diamonds and hourglass shapes.

All Number Patterns →
Did you know?

Most diamond patterns are just two triangles back-to-back: one increasing, one decreasing. Once you see that, many pattern problems become much easier.

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