-2

I have just take the format and spacing but can any one tell me a single change to get the values which print in actual pascal triangle program in java...

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package patterns;

/**
 *
 * @author Love Poet
 */
public class p11 {

    public static void main(String args[]) {
        int row, col, count = 0;
        for (row = 1; row <= 5; row++) {
            for (col = 1; col <= 9; col++) {
                if (((row + col) % 2 == 0) && (row + col >= 6)) {
                    System.out.print("* ");
                    count++;
                } else if (count == row) {
                    break;
                } else {
                    System.out.print("  ");
                }
            }
            count = 0;
            System.out.println();
        }
    }
}

My Output:

        * 
      *   * 
    *   *   * 
  *   *   *   * 
*   *   *   *   * 

What I want:

             1              
         1       1          
      1      2       1      
   1     3       3      1   
1     4      6       4      1
Cœur
  • 37,241
  • 25
  • 195
  • 267
Love Poet
  • 19
  • 1
  • 1
  • 7

1 Answers1

0

I think you followed the second codes. Just follow the first codes from that site:

public class PascalTriangle {
    public static void main(String[] args) {
        int rows = 10;
        for (int i = 0; i < rows; i++) {
            int number = 1;
            System.out.format("%" + (rows - i) * 2 + "s", "");
            for (int j = 0; j <= i; j++) {
                System.out.format("%4d", number);
                number = number * (i - j) / (j + 1);
            }
            System.out.println();
        }
    }
}

Output:

                   1
                 1   1
               1   2   1
             1   3   3   1
           1   4   6   4   1
         1   5  10  10   5   1
       1   6  15  20  15   6   1
     1   7  21  35  35  21   7   1
   1   8  28  56  70  56  28   8   1
 1   9  36  84 126 126  84  36   9   1
Metris Sovian
  • 254
  • 4
  • 20