Descending Numbers with Diagonal Asterisk in C

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

What You’ll Learn

How to print descending numbers (5 to 1) on each row, and replace one diagonal position with * using a simple condition inside a nested loop.

This is a great practice problem for combining nested loops with if/else logic.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
5432*
543*1
54*21
5*321
*4321
1

Complete C Program

We loop j from 5 down to 1 and print * only when the diagonal condition is met.

c
#include <stdio.h>

int main() {
    int i, j;

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

    return 0;
}

🧠 How It Works

1

Outer loop controls rows

for (i = 1; i <= rows; i++) prints one line per row.

Row control
2

Inner loop prints 5..1

for (j = rows; j >= 1; j--) prints descending values on each row.

Number printing
3

Condition draws the diagonal

When j == i, we print * instead of the number. That creates the diagonal line.

Diagonal
=

Diagonal asterisk effect

Exactly one position per row becomes *, creating a diagonal across the output.

2

Variation — User Input Version

This version reads rows and keeps the same diagonal condition.

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 = rows; j >= 1; j--) {
            if (j == i) {
                printf("*");
            } else {
                printf("%d", j);
            }
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Change the diagonal direction with j == (rows + 1 - i)
  • Use a different marker like X or #
  • Add spaces between numbers for readability
  • Print ascending numbers instead of descending
  • Add a second diagonal to form an X-shape

Avoid

  • Forgetting the newline after each row
  • Using the wrong diagonal rule for your chosen direction
  • Printing extra characters when you only want one *
  • Not validating user input when accepting rows

Key Takeaways

1

The inner loop prints descending numbers from rows to 1.

2

A single condition j == i replaces one position per row with *.

3

Time complexity is O(n²) due to the nested loops.

4

Changing the condition changes the diagonal direction and shape.

❓ Frequently Asked Questions

Every row prints descending digits (5 to 1), but one diagonal position is replaced by *, producing a diagonal asterisk line across the output.
It selects exactly one column per row. When that match happens, we print * instead of the number, creating the diagonal.
Use j == (rows + 1 - i). This draws the diagonal from top-left to bottom-right instead.
O(n²) for n rows because we loop through a full row of columns for every row.

Explore More C Number Patterns!

Practice more diagonals, triangles, and pyramids to master nested loops.

All Number Patterns →
Did you know?

Changing just one condition inside the inner loop can turn a plain number grid into diagonals, X-shapes, borders, and many other classic patterns.

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