Question: Add the method public static void printPascalsTriangle(int limit) that prints rows from Pascal's Triangle up to and including limit. All this method will do is
Add the method public static void printPascalsTriangle(int limit) that prints rows from Pascal's Triangle up to and including limit. All this method will do is call printPascalsHelper(limit, 0, 0). The real magic happens in the helper method, private static void printPascalsHelper(int limit, int n, int k). You should use binomialCoefficient to aid in the implementation. This helper method must be recursive, or no credit. No for loops allowed.
In the main method, call the method printPascalsTriangle with a limit of 5.
You should see (trailing whitespace is OK)
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Hints:
Note that each row of Pascals Triangle corresponds to a bunch of (n C k) values. The first line is just (0 C 0), the second is (1 C 0) and (1 C 1), 3rd line is comprised of (2 C 0) (2 C 1) and (2 C 2), and so forth.
Additionally, the different cases you need to account for is if n == k and if n < k. If n == k, you need to print the n choose k and also a new line character, then recurse to print the first entry of the next row of Pascal's triangle. However, you will only recurse to the next row of Pascal's Triangle if the limit has not been reached. On the other hand, if n < k, then you need to print n choose k with a space, and no new line character, then recurse to print out the next binomial coefficient contained in the current row of Pascal's triangle.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
