.
Previous Next

Write a Java program to print pascal triangle

/* Q. Write a Java Program to print Pascal Triangle */
public class PascalTriangle {
     public static void main(String[] args) {
            int rows = 10; /* give input here*/
            for (int i = 0; i < rows; i++) {
                 int number = 1;
                 System.out.format("%" + (rows - i) * 2 + "s", ""); 
                /* to keep distance from initial*/
                 for (int j = 1; j <= i; j++) {
                      System.out.format("%4d", number);
                      number = number * (i - j) /j;
                 }
                 System.out.println();
            }
      }
}

Previous Next