Star Pyramid Pattern

Given a positive integer n, print a centered pyramid of stars (*) with n rows. For each row i (where i ranges from 1 to n): - Print exactly n - i leading…

Given a positive integer `n`, print a centered pyramid of stars (`*`) with `n` rows. For each row `i` (where i ranges from 1 to n): - Print exactly `n - i` leading spaces to center the row. - Print exactly `2 * i - 1` asterisks. Each row adds two more stars and removes one leading space than the row above it, forming a symmetric triangle that grows wider toward the base. 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 `n` rows with no trailing spaces on any line.

Example 1: input n = 3; output * *** *****. Row 1 gives 2 leading spaces + 1 star = ' *'; row 2 gives 1 leading space + 3 stars = ' ***'; row 3 gives 0 leading spaces + 5 stars = '*****'. Total rows = 3.

Example 2: input n = 4; output * *** ***** *******. Row i has (4 - i) leading spaces and (2*i - 1) stars, producing 1, 3, 5, 7 stars across the four rows. The base row has 7 stars and no leading spaces.

Example 3: input n = 1; output *. n = 1 produces a single row — 0 leading spaces + 1 star = '*'. The loop runs exactly once.

Category: Pattern Programs. Topic: pattern-programs. Difficulty: Easy. Patterns: math.