Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Pascal's Triangle Correct Alignment

807597Oct 5 2005 — edited Oct 5 2005
Hello, I have programmed a proper working Pascal's triangle (numerically). But my alignment is flawed. Basically, the main method repeatedly asks the user to enter an integer, inRow, between 0 and 20 (inclusive). For all those integers inputted, a triangle method will output Pascal's triangle up to inRow th row. But I'm trying to align the triangle so that the leftmost number (the first number in the last row) aligns itself to the extreme left of the screen and builds it self up accordingly.
Also, if I try to get the ouput for 10 rows or over, the sides look like they're curving instead of going straight down.

Here's my code:
class Pascal {
  
  static final int WIDTH = 80;                                    //output width
  
  public static void main(String[] args) {
      
    //Main method
    int inRow;
    do {
      System.out.println("What is the number of rows in Pascal's Triangle?");
      inRow = In.getInt();
      if (inRow >= 0 && inRow <= 20) {
          triangle(inRow);
      }
    }
    while (inRow >= 0);
  }
  
  //triangle method (no return value)
  public static void triangle (int row) {
      
    for (int n = 0; n < row; n++) {
        String output = "" + choose(n,0);                  //start output string
        
      for (int r = 1; r <= n; r++) {
          output += " " + choose(n,r);                 //attach to output string
      }

      int gap = (WIDTH - output.length()) / 2;      //number of spaces to center
      
      while (gap > 0) {
          System.out.print(" ");                               //print one space
          gap--;                                    //decrement remaining spaces
      }
      System.out.println(output);                          //print output string
    }

  } //TERMINATE triangle
  
  //choose method
  public static long choose(int n, int r) {
    int upper, lower;                            //higher and lower of r and n-r
    long result = 1;                                    //initialize result to 1
    
    upper = Math.max(r, n-r);                   //larger of the bottom two terms
    lower = Math.min(r, n-r);                  //smaller of the bottom two terms
    
    //process the part of n! greater than upper!
    for (int i = n; i > upper; i--) 
      result *= i;                                         //n*(n-1)*...*(n-r+1)
    
    //eliminate lower! from result
    for (int i = lower; i > 1; i--)
      result /= i;                   //[n*(n-1)*...*(n-r+1)]/lower/lower-1/.../1
    
    return result;
  } //TERMINATE choose
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 2 2005
Added on Oct 5 2005
1 comment
330 views