A mixed number triangle builds each row from two halves: descending i..2, then ascending 1..(rows - i + 1). Every row has exactly rows digits — with rows = 5 you get 12345, 21234, 32123, 43212, 54321.
Unlike Program 49 (products i*j), this shape concatenates digit sequences — no multiplication.
Approach
How to Solve It
Outer loop picks row i. Two inner loops fill the descending half, then the ascending half.
Method
Idea
Best for
Two inner loops
Descending j = i..2, then ascending k = 1..(rows-i+1)
Learning, interviews, demos
Compact trace
Same logic with rows = 3
Quick dry-runs on paper
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i down to 2:
print j (no newline)
for k from 1 to (rows - i + 1):
print k (no newline)
print newline
Cheat sheet
Goal
Pattern
Outer rows
for (i = 1; i <= rows; i++)
Descending half
for (j = i; j > 1; j--) cout << j;
Ascending half
for (k = 1; k <= rows + 1 - i; k++) cout << k;
End the row
cout << "\n";
Digits per row
Always rows
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << j / cout << k
Stays on the same line
Each digit
cout << "\n"
Ends the current line
After both inner loops
Print digits without a newline, then end the row once. Putting "\n" inside either inner loop turns the pattern into a vertical list.
Try it
Live Preview
Change the row count and the mixed triangle updates instantly.
Use rows from 1 to 9 (single digits stay tidy). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 25 digits
12345
21234
32123
43212
54321
Trace
Worked Walkthrough
Trace row i = 3 with rows = 5 — descending then ascending.
Half
Loop
Prints
Line so far
Descending
j = 3, 2
3, 2
32
Ascending
k = 1..(5-3+1) = 1..3
1, 2, 3
32123
When i = 1, the descending loop never runs — the whole row is just 1..rows. When i = rows, the ascending loop prints only 1, so the row is rows..(2)1.
Code
C++ Programs
Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — descending half, then ascending half, on every row.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; i++)
{
for (j = i; j > 1; j--)
cout << j;
for (k = 1; k <= rows + 1 - i; k++)
cout << k;
cout << "\n";
}
return 0;
}
Output
12345
21234
32123
43212
54321
How It Works
1. Outer loop picks the row.i runs from 1 to 5 — one full line per value.
2. Descending half first.j runs from i down past 1, printing i..2 (skipped when i = 1).
3. Ascending half fills the rest.k runs from 1 to rows + 1 - i, then "\n" ends the row.
Example 2 — User Input Rows
Read the row count at runtime — both halves scale automatically.
C++
#include <iostream>
using namespace std;
int main()
{
int rows, i, j, k;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = 1; i <= rows; i++)
{
for (j = i; j > 1; j--)
cout << j;
for (k = 1; k <= rows + 1 - i; k++)
cout << k;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1234
2123
3212
4321
How It Works
1. Same two-loop core. Only the source of rows changes from a literal to cin.
2. Width follows rows. Entering 4 yields four digits on every line.
3. Validate in real apps. Prefer checking cin failure and requiring rows >= 1 (tip below).
Safer input tip
if (!(cin >> rows) || rows < 1)
{
cout << "Please enter a positive integer.\n";
return 1;
}
Example 3 — Compact rows = 3
Smaller height for quick tracing on paper or in interviews.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j, k;
for (i = 1; i <= rows; i++)
{
for (j = i; j > 1; j--)
cout << j;
for (k = 1; k <= rows + 1 - i; k++)
cout << k;
cout << "\n";
}
return 0;
}
Output
123
212
321
How It Works
1. Only three rows. Easy to dry-run every descending and ascending step by hand.
2. Same formula. Nothing changes except rows — proving the pattern scales.
3. Last row is fully descending. Row 3 prints 321 — 3 2 then 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
Descending bound
Stop at 2, not at 1
Use j > 1 (or j >= 2). Going down to 1 duplicates the leading 1 from the ascending half.
Ascending length
Use rows + 1 - i
Wrong bounds break the fixed width of rows digits per line.
Newline
Do not put "\n" inside either inner loop
Call cout << "\n" only after both halves finish.
Bad cin
Validate input
Check cin >> rows and require rows >= 1 before the loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–3)
O(n²)
O(1)
Each of n rows prints exactly n digits, so total work is n². Only a few integers of extra memory.
Remember
Key Takeaways
Rule: print i..2, then 1..(rows - i + 1).
Fixed width: every row has exactly rows digits.
Break the row: call cout << "\n" only after both inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each row i, print descending i..2 then ascending 1..(rows-i+1), then end the line.
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 j = i; j > 1 never runs. Only the ascending loop prints 1..rows.
Program 49 prints i*j products on each row. Program 50 concatenates digit sequences — descending then ascending — with no multiplication.
Printing a digit stays on the same line. Printing a newline ends the current row. Digits use cout without a newline; the row break uses cout << "\n" after both inner loops.
O(n²) for n rows because each row prints n digits and there are n rows.
Both work for the descending loop — they print the same i..2 sequence.
After cin >> rows, check failure and require rows >= 1 before the loops.
🤔
Did you know?
Each row combines two sequences: descending i..2, then ascending 1..(rows-i+1). Every row has exactly rows digits — total prints = n², time O(n²).