C# program to print the Pascal's triangle with user input height

C# program to print the Pascal’s triangle:

In this post, we will learn how to print the Pascal’s triangle in C#. This program will take the height of the triangle as an input from the user and print the triangle of that height. The Pascal’s triangle is a number triangle where each digit in any row is equal to the sum of the left and right digits of the above row. The start and end digits of any row should be 1.

For example, the following triangle is a Pascal’s triangle of height 5:

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

Here, each element of the row is equal to the sum of the left and right elements of the above row and the start and the end elements are 1.

Algorithm to print the Pascal’s triangle:

To understand the algorithm, let’s learn how to print a Pascal’s triangle. Let’s say we want to print the following triangle:

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

If I replace the blank spaces with a *, it will look as like below:

****1 
***1 1 
**1 2 1 
*1 3 3 1 
1 4 6 4 1
  • We will use one outer for loop and two inner for loops.
  • Let’s say height is the height of the triangle.
  • The outer loop will point to each row of the triangle. It will run for height number of times.
  • The first inner loop will print the initial blank spaces and the second loop will print the digits of the triangle.
    • The outer loop will run from 0 to height - 1.
    • The first inner loop will run from 0 to n - i - 1. It will print the initial blank spaces.
    • The second inner loop will run from k = 0 to k = i. It will print 1 if k == 0 or k == i. Else, it will compute the current value by adding the left and the right values of the above row and print it.
  • We will use one 2D array to hold the values of each row.

Let’s write down the program in code.

C# program to print the Pascal’s triangle with user input height:

Below is the complete C# program:

using System;
class ExampleProgram
{
    public static void Main()
    {
        Console.WriteLine("Enter the height: ");
        int height = Convert.ToInt32(Console.ReadLine());
        int spacing = 6;
        
        int[,] data = new int[height, height];

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < height - i - 1; j++)
            {   
                Console.Write("".PadRight(spacing/2));
            }
 
            for (int k = 0; k <= i; k++)
            {
                if (k == 0 || k == i)
                {
                    data[i, k] = 1;
                }
                else
                {
                    data[i, k] = data[i - 1, k] + data[i - 1, k - 1];
                }
                Console.Write(data[i, k].ToString().PadRight(spacing));
            }
            Console.WriteLine();
        }
    }
}

Here,

  • It asks the user to enter the height. It reads this value and assigns it to the height integer variable.
  • The spacing variable is used to add padding to the right of each print to fix the alignment of the digits.
  • The for loop that uses i points to each row of the triangle.
  • The first inner loop that uses j is used to print the starting blank spaces. And, the second inner loop is used to print the number of the Pascal’s triangle.

If you run this program, it will print output as below:

Enter the height: 
8
                     1     
                  1     1     
               1     2     1     
            1     3     3     1     
         1     4     6     4     1     
      1     5     10    10    5     1     
   1     6     15    20    15    6     1     
1     7     21    35    35    21    7     1 

Enter the height: 
4
         1     
      1     1     
   1     2     1     
1     3     3     1  

C# Pascal's triangle example

You might also like: