This is a C# program to print the pascal’s triangle. This program uses the for loops to print the Pascal’s triangle. Before we move on to the program, let us learn what a Pascal’s triangle really is.

Pascal’s triangle from Wikipedia:

In mathematics, Pascal’s triangle is a triangular array of the binomial coefficients in a triangle. It is named after the French mathematician, Blaise Pascal. It is known as Pascal’s triangle in much of the Western world, although other mathematicians studied it centuries before him in India, Persia, China, Germany, and Italy.

The rows of Pascal’s triangle are conventionally enumerated starting with row n = 0 at the top. 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. A simple construction of the triangle proceeds in the following manner. On row 0, write only the number 1. Then, to construct the elements of following rows, add the number directly above and to the left with the number directly above and to the right to find the new value. If either the number to the right or left is not present, substitute a zero in its place. For example, the first number in the first row is 0 + 1 = 1, whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row

Program:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// C# program to print the pascal's triangle. using System; class Pascal { public static void Main() { int [,]arr = new int[8,8]; //Declaring an array for (int i=0; i<8; i++) { for (int k=7; k>i; k--){ //For loop to print spaces Console.Write(" "); } for(int j=0; j<i; j++) { if(j==0 || i==j) { arr[i,j] = 1; }else { arr[i,j] = arr[i-1,j] + arr[i-1,j-1]; } Console.Write(arr[i,j]+" "); } Console.WriteLine(); } } } |

#### Vlad

#### Latest posts by Vlad (see all)

- Code jam “Tic-Tac-Toe-Tomek” solution in java - April 19, 2013
- Code jam “Minimum Scalar product” solution in java - March 18, 2013
- Code jam Store Credit solution in java - March 10, 2013