

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to create Pascal’s Triangle
A Pascal’s triangle contains numbers in a triangular form where the edges of the triangle are the number 1 and a number inside the triangle is the sum of the 2 numbers directly above it.
A program that demonstrates the creation of the Pascal’s triangle is given as follows.
Example
using System; namespace PascalTriangleDemo { class Example { public static void Main() { int rows = 5, val = 1, blank, i, j; Console.WriteLine("Pascal's triangle"); for(i = 0; i<rows; i++) { for(blank = 1; blank <= rows-i; blank++) Console.Write(" "); for(j = 0; j <= i; j++) { if (j == 0||i == 0) val = 1; else val = val*(i-j+1)/j; Console.Write(val + " "); } Console.WriteLine(); } } } }
Output
The output of the above program is as follows.
Pascal's triangle 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Now, let us understand the above program.
The Pascal’s triangle is created using a nested for loop. The outer for loop situates the blanks required for the creation of a row in the triangle and the inner for loop specifies the values that are to be printed to create a Pascal’s triangle. The code snippet for this is given as follows.
for(i = 0; i<rows; i++) { for(blank = 1; blank <= rows-i; blank++) Console.Write(" "); for(j = 0; j <= i; j++) { if (j == 0||i == 0) val = 1; else val = val*(i-j+1)/j; Console.Write(val + " "); } Console.WriteLine(); }
- Related Questions & Answers
- Java program to print Pascal's triangle
- Program to generate Pascal's triangle in Python
- Java Program to Print Star Pascal's Triangle
- Pascal's Triangle in C++
- Pascal's Triangle II in C++
- Program to find the nth row of Pascal's Triangle in Python
- Finding the elements of nth row of Pascal's triangle in JavaScript
- Python Program to Print the Pascal's triangle for n number of rows given by the user
- How to print integers in the form of Pascal triangle using C?
- Java program to generate and print Floyd’s triangle
- Program to print Reverse Floyd’s triangle in C
- How to print Floyd’s triangle (of integers) using C program?
- Program to create one triangle stair by using stars in Python
- Floyd's triangle in PL/SQL
- Java Program to calculate the area of a triangle using Heron's Formula
Advertisements