Pascal's Triangle
Pascal's Triangle is a triangular arrangement of numbers where each row contains the binomial coefficients for that row index. Row 0 contains only 1. Every…
Pascal's Triangle is a triangular arrangement of numbers where each row contains the binomial coefficients for that row index. Row 0 contains only 1. Every subsequent row begins and ends with 1, and each interior value equals the sum of the two values directly above it in the previous row. Given a positive integer `n`, print the first `n` rows of Pascal's Triangle. Output format: print each row on its own line, with values separated by single spaces. Row indices are zero-based internally, but you print exactly `n` rows in order from row 0 to row n-1. There is no leading or trailing whitespace on any line.
- `1 <= n <= 10` - All values in the triangle fit comfortably in a 32-bit signed integer for the given input range.
Example 1: input n = 4; output 1 1 1 1 2 1 1 3 3 1. Row 0 is always [1]. Row 1 is [1, 1]. Row 2 is [1, 2, 1] because the interior value 2 = 1 + 1 from row 1. Row 3 is [1, 3, 3, 1] because both interior 3s come from summing adjacent pairs of row 2.
Example 2: input n = 6; output 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1. Six rows are printed. Row 5 contains [1, 5, 10, 10, 5, 1]. The 10s in the middle are each derived as 4 + 6 and 6 + 4 from the row above.
Example 3: input n = 2; output 1 1 1. Only two rows are needed. Row 0 is [1] and row 1 is [1, 1] because there are no interior cells yet.
Category: Pattern Programs. Topic: pattern-programs. Difficulty: Easy. Patterns: math.