A mixed number triangle prints every row with two halves: descending digits i..2, then ascending digits 1..(rows - i + 1). Each row has exactly rows digits.
Remember
Rule: for i from 1 to rows
print descending i..2
print ascending 1..(rows - i + 1)
12345
21234
32123
43212
54321 ← rows = 5
Follows the multiplication triangle in Program 49; next is the alternating ascending/descending triangle in Program 51.
Approach
How to Solve It
Use one outer loop and two inner loops per row — descending first, ascending second. End the row with printf("\n").
Method
Idea
Best for
Two inner loops
Descending j = i..2, then ascending k = 1..(rows-i+1)
Learning, interviews, exams
Compact trace
Dry-run with rows = 3 before coding rows = 5
Paper tracing and labs
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i down to 2:
print j
for k from 1 to (rows - i + 1):
print k
print newline
Cheat sheet
Goal
Pattern
Pick each row
for (i = 1; i <= rows; i++)
Descending half
for (j = i; j > 1; j--) printf("%d", j);
Ascending half
for (k = 1; k <= rows + 1 - i; k++) printf("%d", k);
End of row
printf("\n");
Digits per row
Always rows
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", value)
Stays on the same line
Each digit in both halves
printf("\n")
Ends the current line
After both inner loops
Print all digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the mixed triangle updates instantly — capped at 9 so every digit stays a single character.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 5×5 digits
12345
21234
32123
43212
54321
Trace
Worked Walkthrough — Row i = 3, rows = 5
Trace both halves for the middle row — descending 3 2, then ascending 1 2 3.
Half
Loop
Prints
Descending
j = 3, 2
3 then 2
Ascending
k = 1..(5-3+1) = 1..3
123
Full row: 32123. Every row has rows digits → n × n = O(n²) total prints.
Code
C Programs
Three complete programs: fixed rows = 5, scanf input, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — descending i..2 then ascending 1..(rows-i+1) on each line.
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; i++)
{
for (j = i; j > 1; j--)
printf("%d", j);
for (k = 1; k <= rows + 1 - i; k++)
printf("%d", k);
printf("\n");
}
return 0;
}
Output
12345
21234
32123
43212
54321
How It Works
1. Outer loop.i runs from 1 to 5 — each pass is one row.
2. Descending half.j runs from i down past 1 — skipped entirely when i = 1.
3. Ascending half.k runs from 1 to rows + 1 - i, filling the rest of the row to exactly 5 digits.
Example 2 — User Input Rows
Read rows with scanf and reject non-positive values.
C
#include <stdio.h>
int main(void)
{
int rows;
int i, j, k;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows < 1)
{
printf("Please enter a positive integer.\n");
return 1;
}
for (i = 1; i <= rows; i++)
{
for (j = i; j > 1; j--)
printf("%d", j);
for (k = 1; k <= rows + 1 - i; k++)
printf("%d", k);
printf("\n");
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1234
2123
3212
4321
How It Works
1. Prompt and validate. Reject bad input with a clear message.
2. Same core. Only the outer loop limit comes from the user — both inner loops stay identical.
3. Safer input tip. Cap demos to single-digit rows:
Safer input
if (scanf("%d", &rows) != 1 || rows < 1 || rows > 9)
{
printf("Enter a whole number from 1 to 9.\n");
return 1;
}
Example 3 — Compact rows = 3
Same two-loop structure with a smaller height for quick paper tracing.
C
#include <stdio.h>
int main(void)
{
int rows = 3;
int i, j, k;
for (i = 1; i <= rows; i++)
{
for (j = i; j > 1; j--)
printf("%d", j);
for (k = 1; k <= rows + 1 - i; k++)
printf("%d", k);
printf("\n");
}
return 0;
}
Output
123
212
321
How It Works
1. Three rows. Row 1 skips descending and prints 123; row 2 prints 2 then 12; row 3 prints 32 then 1.
2. Trace on paper. Confirm each row has exactly 3 digits before scaling to rows = 5.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
j >= 1
Extra 1 in the middle
If the descending loop runs to 1, you print an extra 1 before the ascending half. Stop at j > 1 (or j >= 2).
wrong bound
Uneven row length
Ascending must use rows + 1 - i. A fixed k <= rows makes later rows too long.
\n inside
Broken rows
If printf("\n") sits inside either half, each digit lands on its own line. Call the newline only after both loops.
rows = 1
Single row
Output is just 1 — descending is skipped, ascending prints one digit.
rows > 9
Multi-digit values
Past 9, values like 10 take two characters and break the tight look. Cap demos at 9 or use spaced / fixed-width format.
scanf
Check the return value
If scanf fails, rows may be uninitialized — always test scanf(...) == 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact rows = 3 (Example 3)
O(n²)
O(1)
Each of n rows prints exactly n digits → n² prints. For n = 5 that is 25 digits.
Remember
Key Takeaways
Two halves: descending i..2, then ascending 1..(rows-i+1).
Fixed width: every row has exactly rows digits — row 1 skips the descending half.
Break the row: call printf("\n") only after both inner loops.
Complexity:O(n²) time from n × n digits; O(1) extra space.
One line: for each i, print i..2 then 1..(rows-i+1), then printf("\n").
Frequently Asked Questions
Each row prints two parts: first a descending sequence from i down to 2, then an ascending sequence from 1 up to (rows - i + 1). Row 2 becomes 2 + 1234 = 21234.
One inner loop prints the descending part (j = i down to > 1). The other prints the ascending part (k = 1..(rows-i+1)). Splitting them keeps the two halves clear.
When i = 1, the descending loop never runs. Only the ascending loop prints 1..rows — a full ascending line.
The descending loop prints i first. For i = 2, that is 2. Then the ascending loop prints 1..(rows-2+1) = 1..4, giving 21234.
Change rows or read it from user input with scanf — see Example 2.
O(n²) for n rows because each row prints n digits and there are n rows — total prints = n².
Program 49 prints i*j products on each row. Program 50 concatenates digit sequences — descending then ascending — with no multiplication.
Both work for the descending loop: for (j = i; j >= 2; j--) and for (j = i; j > 1; j--) print the same i..2 sequence.
🤔
Did you know?
Each row combines two sequences: descending i..2, then ascending 1..(rows-i+1). Row 2 prints 21234; row 5 prints 54321 — still O(n²) total prints for n rows.