Table of Contents
C Sharp Exercise: print Pascal triangle pattern
In this tutorial, we will discuss the title of C Sharp Exercise: print Pascal triangle pattern
In this post, we are going to learn how to display the pascal triangle number pattern in C# language using for and while loop,
Program to generate pascal’s triangle
Program to display pascal triangle Using for loop
Program 1
The program allows the user to enter the number of rows and then the program will show the pascal triangle number pattern using for loop in the C# programming language according to the rows
//Print pascal triangle number pattern //using for loop using System; namespace PascalTriangle{ public class pascalPatternFor { public static void Main(string[] args) { //decare variabes as integers int rowNo,i,j,num=1; //Take input from user Console.Write ("Enter the numbers To rows: "); rowNo=Convert.ToInt32( Console.ReadLine()); //reading the input rom user //move to new line Console.WriteLine (" "); //dispay the pattern for(i=1; i<=rowNo; i++){ //initialize first coefficient num=1; //print space for(j=1; j<=rowNo-i; j++) Console.Write (" "); //print number for(j=1; j<=i; j++){ Console.Write (num+" "); //print number with spaces //calculate the next coefficient num=num*(i-j)/j; } //move to new iine Console.WriteLine (" "); } Console.ReadKey(); } } }
When the above code is executed, it produces the following result
Program to display pascal triangle Using while loop
Program 1
The program allows the user to enter the number of rows and then the program will show the pascal triangle number pattern using a while loop in the C# programming language
//Print pascal triangle number pattern //using for loop using System; namespace PascalTriangle{ public class pascalPatternFor { public static void Main(string[] args) { //decare variabes as integers int rowNo,i,j,num=1; //Take input from user Console.Write ("Enter the numbers To rows: "); rowNo=Convert.ToInt32( Console.ReadLine()); //reading the input rom user //move to new line Console.WriteLine (" "); //dispay the pattern i=1; while(i<=rowNo){ //initialize first coefficient num=1; j=1; //print space while(j<=rowNo-i){ Console.Write (" "); j++; } //print number j=1; while(j<=i){ Console.Write (num+" "); //print number with spaces //calculate the next coefficient num=num*(i-j)/j; j++; } //move to new iine Console.WriteLine (" "); i++; } Console.ReadKey(); } } }
When the above code is executed, it produces the following result
Similar post
C program to print pascal triangle
C++ program to print pascal triangle
Java program to print pascal triangle
C program to print pascal triangle using 1 D array
C++ program to print pascal triangle using 1 D array
Java program to print pascal triangle using 1 D array
Java program to print pascal triangle using 2 D array
Java program to triangle number pattern
Java program to triangle number pattern using while loop
Java program to pyramid triangle star pattern