1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.Scanner;
 
class Proj3
{
       public static void main(String[] args) 
       {
              Scanner s = new Scanner(System.in);
 
              System.out.print("Enter a positive integer bound: ");
              int num = Integer.parseInt(s.nextLine());
              System.out.println("Pascal's Triangle:\n");
 
              // Start iterating through numbers of Pascal's Triangle
              for (int i = 0; i <= num; i++)
              {
                     // In order to make table look good, calculate the
                     // spacing needed to center it.
                     for (int space = num - i; space > 0; space--)
                     {
                            System.out.print("  ");
                     }
 
                     for (int j = 0; j <= i; j++)
                     {
                            int value = getFactorial(i) / (getFactorial(j) * getFactorial(- j));
 
                            // If value is greater than 1 digit, then only add 1 space, otherwise add 2
                            if(Integer.toString(value).length() == 1)
                                   System.out.print("  ");
                            else
                                   System.out.print(" ");
 
                            System.out.print(value);
 
                            // If value is greater than 2 digits, do not add a space after
                            if(Integer.toString(value).length() < 3)
                                   System.out.print(" ");
                     }
                     System.out.println();
              }
 
              // Iteration for Binomial Expansions
              System.out.println("\nBinomial Expansions:\n");
              
              // Always output for expansion by default
              System.out.println("(x + y)^0 = 1");
              for (int i = 1; i <= num; i++)
              {
                     int x = i;
                     System.out.print("(x + y)^" + i + " = ");
                     for (int j = 0; j <= i; j++)
                     {
                            int coef = getFactorial(i) / (getFactorial(j) * getFactorial(- j));
 
                            if(!= 0)
                                   System.out.print(" + ");
                            
                            if (coef > 1)
                                   System.out.print(coef);
 
                            if (> 1)
                                   System.out.print("x^" + x);
                            else if (== 1)
                                   System.out.print("x");
 
                            if (> 1)
                                   System.out.print("y^" + j);
                            else if (== 1)
                                   System.out.print("y");
 
                            x--;
                     }
                     System.out.println();
              }
       }
       
       private static int getFactorial(int num)
       {
              if(num == 0)
                     return 1;
 
              int returnval = num;
 
              for (int i = num - 1; i > 0; i--)
                     returnval *= i;
 
              return returnval;
       }
}