Print Pascals triangle - Java program

Q. Write a program to print numbers in Pascals triangle.

Answer:

Pascals triangle is an interesting number pattern, where the binomial coefficient is arranged in form of triangular array.
In this example, we are using multiple loop to print the pascal triangle.

PascalTriangle.java

class PascalTriangle
{
        public static void main(String[] args)
        {
                int row = 5;
                int number;
                System.out.println("Pascal Triangle:");
                for(int i=0;i<row;i++)
                {
                        for(int k=row; k>i; k--)
                        {
                                System.out.print(" ");
                        }
                        number = 1;
                        for(int j=0;j<=i;j++)
                        {
                                System.out.print(number+ " ");
                                number = number * (i - j) / (j + 1);
                        }
                        System.out.println();
                }
        }
}


Output:

pascal triangle