---
title: "Pascal Triangle"  
description: "Pascal Triangle"  
author: "Steilla Mitchel"  
published: 2024-06-12  
updated: 2024-06-13  
canonical: https://www.mindstick.com/forum/160731/pascal-triangle  
category: "c#"  
tags: ["c#", "programming language", "programming help", "programs"]  
reading_time: 2 minutes  

---

# Pascal Triangle

Given an [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer) `numRows`, return the first numRows of **Pascal's triangle**.

In **Pascal's triangle**, each number is the [sum](https://www.mindstick.com/forum/159332/how-can-i-use-sum-in-linq) of the two numbers directly above it as shown:

## Example 1:

**[Input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java):** numRows = 5 **[Output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular):** [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

## Example 2:

**Input:** numRows = 1 **Output:** [[1]]

## Replies

### Reply by Ashutosh Patel

#### Pascal's Triangle

Pascal’s triangle is a geometric arrangement of binomial factors in a square. Each number in a square is the sum of the two directly above it. It is named after the French mathematician Blaise Pascal, although he was known in various parts of the world long before his time.

## Sample of Pascal's triangle with 5 rows

```plaintext
                 1
              1     1
           1     2     1
        1     3     3     1
     1     4     6     4     1
```

The number at the edge of the triangle is always 1, and each inner number is the sum of the two digits above it. This process is continuous.

## Example-

Here is a basic C# example to demonstrate Pascal's triangle,

```cs
using System;
namespace MindStickSoft
{
   class Program
   {
       public static void Main(string[] args)
       {
           Console.WriteLine("Enter the number of rows for Pascal's Triangle");
           int rowNum = int.Parse(Console.ReadLine());
           PrintTriangle(rowNum);
           Console.ReadLine();
       }
       static void PrintTriangle(int rownumber)
       {
           for (int i = 0; i < rownumber; i++)
           {
               int number = 1;
               for (int j = 0; j < rownumber - i; j++)
               {
                   Console.Write("   ");
               }
               for (int k = 0; k <= i; k++)
               {
                   Console.Write("   {0:D}  ", number);
                   number = number * (i - k) / (k + 1);
               }
               Console.WriteLine();
           }
       }
   }
}
```

## Output-

```plaintext
Enter the number of rows for Pascal's Triangle
5
                 1
              1     1
           1     2     1
        1     3     3     1
     1     4     6     4     1
```

**Also, Read:** [What is out keyword in C#?](https://www.mindstick.com/forum/160715/what-is-out-keyword-in-c-sharp)


---

Original Source: https://www.mindstick.com/forum/160731/pascal-triangle

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
