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,
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