Descending Numbers with Diagonal Asterisk in C

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:
5432*
543*1
54*21
5*321
*4321Complete C Program
We loop j from 5 down to 1 and print * only when the diagonal condition is met.
#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
Outer loop controls rows
for (i = 1; i <= rows; i++) prints one line per row.
Inner loop prints 5..1
for (j = rows; j >= 1; j--) prints descending values on each row.
Condition draws the diagonal
When j == i, we print * instead of the number. That creates the diagonal line.
Diagonal asterisk effect
Exactly one position per row becomes *, creating a diagonal across the output.
Variation — User Input Version
This version reads rows and keeps the same diagonal condition.
#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
Xor# - 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
The inner loop prints descending numbers from rows to 1.
A single condition j == i replaces one position per row with *.
Time complexity is O(n²) due to the nested loops.
Changing the condition changes the diagonal direction and shape.
❓ Frequently Asked Questions
* instead of the number, creating the diagonal.j == (rows + 1 - i). This draws the diagonal from top-left to bottom-right instead.Explore More C Number Patterns!
Practice more diagonals, triangles, and pyramids to master nested loops.
Changing just one condition inside the inner loop can turn a plain number grid into diagonals, X-shapes, borders, and many other classic patterns.
12 people found this page helpful
