Tuesday 27 October 2015

Pascal's Triangle Using JAVA

Pascal's Triangle Using JAVA


In mathematics, Pascal's triangle is a triangular array of the binomial coefficients. In much of the Western world it is named after French mathematician Blaise Pascal, although other mathematicians studied it centuries before him in India, Iran, China, Germany, and Italy.
The rows of Pascal's triangle (sequence A007318 in OEIS) are conventionally enumerated starting with row n = 0 at the top (the 0th row). The entries in each row are numbered from the left beginning with k = 0 and are usually staggered relative to the numbers in the adjacent rows. Having the indices of both rows and columns start at zero makes it possible to state that the binomial coefficient \tbinom{n}{k} appears in the nth row and kth column of Pascal's triangle. A simple construction of the triangle proceeds in the following manner: In row 0, the topmost row, the entry is \tbinom{0}{0} = 1 (the entry is in the zeroth row and zeroth column). Then, to construct the elements of the following rows, add the number above and to the left with the number above and to the right of a given position to find the new value to place in that position. If either the number to the right or left is not present, substitute a zero in its place. For example, the initial number in the first (or any other) row is 1 (the sum of 0 and 1), whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row.
This construction is related to the binomial coefficients by Pascal's rule, which says that if
(x+y)^n=\sum_{k=0}^n{n \choose k}x^{n-k}y^{k}
then
 {n \choose k} = {n-1 \choose k-1} + {n-1 \choose k}
for any non-negative integer n and any integer k between 0 and n.
Pascal's triangle has higher dimensional generalizations. The three-dimensional version is called Pascal's pyramid or Pascal's tetrahedron, while the general versions are called Pascal's simplices.

Program Using JAVA:

public class pascal{
public static void main(String args[]){
int row=10;

for(int i=0;i<row;i++){
int no=1;
System.out.format("%"+(row-i)*2+"s","");
for(int j=0;j<=i;j++){
System.out.format("%4d",no);
no=no*(i-j)/(j+1);
}
System.out.println();
}
}
}

Download Program File: Click Here

Output:


No comments:

Post a Comment