Program: Write a program to enter number of rows and create a Pascal Triangle.
Input: 5
Output:

     1               row=1
    1 1              row=2
   1 2 1             row=3
  1 3 3 1            row=4
 1 4 6 4 1           row=5



Source FilePascalTriangle.java

package triangle;
public class PascalTriangle
{
      public static void main(String args[])
      {
            int rows, i, k, number=1, j;
            rows = 5;  //change number of rows as per need
            for(i=0;i<rows;i++)
            {
                  for(k=rows; k>i; k–)
                  {
                        System.out.print(” “);
                  }
                  number = 1;
                  for(j=0;j<=i;j++)
                  {
                        System.out.print(number+ ” “);
                        number = number * (ij) / (j + 1);
                  }
                  System.out.println();
            }
      }
}



OUTPUT:

     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1



 You may also like to read about:
  • Star Pattern Program in java
  • Can we call run() directly on thread without calling start?
  • Class variable vs instance variable
  • iterator vs list iterator
  • Why String is immutable in java?
  • What is marker interface?

Source link

Pascal triangle in java

Leave a Reply

Your email address will not be published. Required fields are marked *