Diamond Pattern
Given a positive integer n, print a star diamond made entirely of asterisks (*) and spaces. The diamond has a total of 2n - 1 rows. The upper pyramid spans…
Given a positive integer `n`, print a star diamond made entirely of asterisks (`*`) and spaces. The diamond has a total of `2n - 1` rows. The upper pyramid spans rows 1 through n (inclusive) and the lower inverted pyramid mirrors it from row n-1 back down to row 1. The exact rule for each row `i` (where i ranges from 1 to n): - Print exactly `n - i` leading spaces. - Print exactly `2 * i - 1` asterisks, centered under the widest row. The lower half repeats this rule for decreasing values of i from n-1 down to 1, producing a symmetric reflection of the upper pyramid below the widest row. There must be no trailing spaces on any row. Join all rows with a single newline character and do not add a trailing newline at the end.
- `1 ≤ n ≤ 10` - `n` is always a positive integer. - The output must contain exactly `2n - 1` rows with no trailing spaces on any line.
Example 1: input n = 3; output * *** ***** *** *. Upper pyramid — row 1 gives 2 spaces + 1 star; row 2 gives 1 space + 3 stars; row 3 gives 0 spaces + 5 stars. Lower pyramid mirrors rows 2 and 1. Total rows = 2*3 - 1 = 5.
Example 2: input n = 4; output * *** ***** ******* ***** *** *. Upper pyramid rows 1 to 4 — each row i has (4-i) leading spaces and (2*i-1) stars. Lower pyramid mirrors rows 3, 2, 1 below the widest row. Total rows = 2*4 - 1 = 7.
Example 3: input n = 1; output *. n = 1 produces a single row — 0 spaces + 1 star. The lower half loop (n-1 down to 1) has no iterations.
Category: Pattern Programs. Topic: pattern-programs. Difficulty: Medium. Patterns: math.