
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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 Articles
- 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
- Python Program to Print the Pascal's triangle for n number of rows given by the user
- Finding the elements of nth row of Pascal's triangle in JavaScript
- What does Pascal's law states?
- How to print integers in the form of Pascal triangle using C?
- Program to create one triangle stair by using stars in Python
- Java Program to calculate the area of a triangle using Heron's Formula
- Relation Between Bar and Pascal
- Floyd's triangle in PL/SQL
- Python program to print number triangle

Advertisements