Trace how shrinking i adds more ** pairs while keeping the mirror.
i
Left
** pairs
Right
Prints
5
12345
0
54321
1234554321
4
1234
1
4321
1234**4321
3
123
2
321
123****321
2
12
3
21
12******21
1
1
4
1
1********1
Star pairs per row = n - i. Each row still has width proportional to n → O(n²) total work.
Code
C++ Programs
Three complete programs: fixed n = 5, cin input, and a compact n = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed n = 5
Hard-coded size — ascending digits, ** pairs, then descending digits.
C++
#include <iostream>
using namespace std;
int main()
{
int n = 5;
int i, j, k, m;
for (i = n; i >= 1; i--)
{
for (j = 1; j <= i; j++)
cout << j;
for (k = i; k < n; k++)
cout << "**";
for (m = i; m >= 1; m--)
cout << m;
cout << "\n";
}
return 0;
}
1. Outer descends.i starts at n (full digits, no stars) and shrinks toward 1.
2. Three parts. Print 1..i, then n - i star pairs, then i..1.
3. Newline once. Call cout << "\n" only after all three inner loops finish.
Example 2 — User Input Size
Read n with cin, validate, then use the same three-loop core.
C++
#include <iostream>
using namespace std;
int main()
{
int n;
int i, j, k, m;
cout << "Enter n: ";
if (!(cin >> n) || n <= 0)
{
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = n; i >= 1; i--)
{
for (j = 1; j <= i; j++)
cout << j;
for (k = i; k < n; k++)
cout << "**";
for (m = i; m >= 1; m--)
cout << m;
cout << "\n";
}
return 0;
}
Output (when user enters 3)
Enter n: 3
123321
12**21
1****1
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Ascending + stars + descending matches Example 1 — only n comes from the user.
3. Safer input tip. Cap demos for readable output:
Safer input
if (!(cin >> n) || n < 1 || n > 9)
{
cout << "Enter a whole number from 1 to 9.\n";
return 1;
}
Example 3 — Compact n = 3
Same structure with only three rows — easy to confirm left, center, and right.
C++
#include <iostream>
using namespace std;
int main()
{
int n = 3;
int i, j, k, m;
for (i = n; i >= 1; i--)
{
for (j = 1; j <= i; j++)
cout << j;
for (k = i; k < n; k++)
cout << "**";
for (m = i; m >= 1; m--)
cout << m;
cout << "\n";
}
return 0;
}
Output
123321
12**21
1****1
How It Works
1. Three rows.i = 3 → 123321; i = 2 → 12**21; i = 1 → 1****1.
2. Trace on paper. If you skip the descending loop, the row is not mirrored — keep all three inners.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for n = 5 or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
no mirror
Missing descending loop
Without for (m = i; m >= 1; m--) the row is not mirrored. Keep all three inner loops.
single *
Wrong center growth
Printing "*" instead of "**" grows the center too slowly and breaks symmetry with the digit halves.
\n inside
Broken rows
If cout << "\n" sits inside any inner loop, the mirror splits across lines. Call it only after all three finish.
n = 1
Single mirror
Output is just 11 — no stars. A good sanity check for input validation.
k bound
Star count off
Use for (k = i; k < n; k++) — exactly n - i pairs. A wrong bound misaligns the center.
cin
Check the stream
Validate cin >> n before looping — a failed read leaves n unset or stale.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact n = 3 (Example 3)
O(n²)
O(1)
Each of n rows prints O(n) characters (digits + stars) → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Three parts: ascending 1..i, star pairs, descending i..1.
Grow the center: print "**" exactly n - i times as i shrinks.
Break the row: digits and stars in the inners; cout << "\n" after all three.
Complexity:O(n²) time from n rows of O(n) characters; O(1) extra space.
One line: for each i from n down to 1, print 1..i, then ** pairs, then i..1, then newline.
Frequently Asked Questions
A number and asterisk mirror: for n=5 you get 1234554321 / 1234**4321 / 123****321 / 12******21 / 1********1 — ascending digits, growing ** center, then descending digits.
Each iteration prints two asterisks so the center grows by 2 characters per row while keeping the pattern symmetric.
The pattern prints ascending numbers 1..i, then stars, then descending numbers i..1 on the same line.
Because i starts at n and decreases — each row prints fewer digits and more stars in the center.
cout << j stays on the same line. cout << "\n" ends the current line. Digits and stars use cout; the row break uses cout << "\n" after all three inner loops.
Three — ascending digits (j), star pairs (k), and descending digits (m).
Yes. Replace "**" with " " or "##" — digit loops stay the same.
O(n²) for size n because each of n rows prints O(n) characters overall.
🤔
Did you know?
Each row prints 1..i, then a growing block of ** pairs, then i..1 — three inner loops create a symmetric mirror. As i shrinks, the star block grows to keep row width consistent.