Butterfly Pattern

Given a positive integer n, print a butterfly shape made entirely of asterisks (*) and spaces. The butterfly has a total of 2n - 1 rows. The upper half spans…

Given a positive integer `n`, print a butterfly shape made entirely of asterisks (`*`) and spaces. The butterfly has a total of `2n - 1` rows. The upper half spans rows 1 through n (inclusive) and the lower half 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 `i` asterisks on the left. - Print exactly `2 * (n - i)` spaces in the middle. - Print exactly `i` asterisks on the right. The lower half repeats this rule for decreasing values of i from n-1 down to 1, producing a symmetric reflection of the upper half 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 half — row 1 gives 1 star + 4 spaces + 1 star; row 2 gives 2 stars + 2 spaces + 2 stars; row 3 gives 3 stars + 0 spaces + 3 stars. Lower half mirrors rows 2 and 1. Total rows = 2*3 - 1 = 5.

Example 2: input n = 4; output * * ** ** *** *** ******** *** *** ** ** * *. Upper half rows 1 to 4. Lower half 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 — 1 star + 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.