Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(i.e. MUST) a method that displays Pascal\'s Triangle. In addition, you are to s

ID: 3598075 • Letter: #

Question

(i.e. MUST) a method that displays Pascal's Triangle. In addition, you are to solicit from the User the number of rows in the triangle, range 1 - 12 (inform the User of this range) and use that value as a parameter to be passed to the method Observation The values between the 1s in each row are derived from the sum of two values in the previous row of the triangle The rows and columns of the triangle represent the coefficients of the xk term in the polynomial expansion of the binomial power (1 + x)n, where n is the row in the triangle and k is the column in the row, each starting at O The equation for a given position, k, in row n of the triangle is given by the formula n1/ (k! * (n-k)! ), where 0

Explanation / Answer

package emp;

import java.util.Scanner;

public class Merge {

   public static void print(int row) {
       for (int i = 0; i < row; i++) {
         
           for(int j=row-i;j>=1;j--){
               System.out.print(" ");
           }
         
           for (int k = 0; k <= i; k++) {
               System.out.print(nCk(i, k) + " ");
           }
           System.out.println();
       }
   }

   public static int nCk(int n, int k) {
       int numerator = factorial(n);
       int denominator = factorial(k) * factorial(n - k);
       int result = (int) (numerator / denominator);
       return result;
   }

   public static int factorial(int num) {
       int result = 1;
       for (int i = 1; i <= num; i++) {
           result = result * i;
       }
       return result;
   }

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.print("Enter the number of rows want to display Pascal's triangle : ");
       int row = scanner.nextInt();
       print(row);
   }
}


/* output:-

Enter the number of rows want to display Pascal's triangle : 6
      1
     1 1
    1 2 1
   1 3 3 1
1 4 6 4 1
1 5 10 10 5 1


*/