C# Program to Print Number Triangle


This is a C# Program to display numbers in the form of triangle.

Problem Description

This C# Program Displays Numbers in the form of Triangle.

Problem Solution

Here the numbers are displayed in the form of a triangle.

Program/Source Code

Here is source code of the C# Program to Display Numbers in the form of Triangle. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 *  C# Program to Display Numbers in the form of Triangle
using System;
class Pascal
    public static void Main()
        int[,] arr = new int[8, 8];     
        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;
                    arr[i, j] = arr[i - 1, j] + arr[i - 1, j - 1];
                Console.Write(arr[i, j] + " ");
Program Explanation

In this C# program, we are printing the numbers in the form of triangle. Pascal’s triangle is a triangular array of the binomial coefficients. The program consists of six integer type of variables named i, j, rows, array[][], k and num respectively. Out of this variable i, j and k have been defined to control the for loop, the integer ‘rows’ stores the limit of Pascal’s triangle entered by the user.

Note: Join free Sanfoundry classes at Telegram or Youtube

As the C program for Pascal’s triangle is executed, it first asks for the value of limit of the triangle. The program assigns ‘rows’ variable value with the value of ‘i’ variable; i.e., number of space with the limit of Pascal’s triangle, for loop in which ‘i’ is the loop control variable. Again, in order to control the space, a nested for loop with ‘k’ as a control variable is used.

Finally, for printing the elements in this program for Pascal’s triangle in C, another nested for loop of control variable ‘j’ has been used. The formula used to generate the numbers of Pascal’s triangle is:
Pascal triangle: array[i][j] = array[i – 1][j – 1] + array[i – 1][j].

Runtime Test Cases
     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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Take C# Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.