Friday, 1 April 2016

Print Pascal Triangle Program in Java

Print Pascal Triangle Program in Java

Hello friends here we will write simple code for Print Pascal Triangle Program in Java. Pascal’s triangle is a set of numbers arranged in the form of a triangle. Each number in a row is the sum of the left number and right number on the above row. If a number is missing in the above row, it is assumed to be 0. The first row starts with number 1. The following is a Pascal triangle with 5 rows,
Pascal Triangle Program in Java

Code

public class PascalTriangleDemo
{
  public static void main(String[] args)
{
     
int n = 10;
int[] row = new int[0];    
 for (int i=0; i<n; i++)
{            
row = pascalRow(row);    
 for (int j=0; j< n - i; j++)
{
System.out.print(" ");
 }    
for (int j=0; j< row.length; j++)
 {
System.out.print(row[j]+" ");
 }
System.out.println();  
}
}
}

No comments:

Post a Comment