﻿ C# exercises and solutions-C# array: Pascal triangle

C# exercises and solutions-C# array: Pascal triangle

C# array exercise: Pascal triangle

Exercise: By using two-dimensional array of C# language, write C# program to display a table that represents a Pascal triangle of any size. In Pascal triangle, the first and the second rows are set to 1. Each element of the triangle (from the third row downward) is the sum of the element directly above it and the element to the left of the element directly above it. See the example Pascal triangle(size=5) below:

 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

Solution:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

int size;

Console.Write("Pascal triangle size:");

size = Int32.Parse(Console.ReadLine());

printPascalTr(size);

Console.ReadLine();

}

public static void printPascalTr(int size){

int[,] PascalTr=new int[size,size];

int row,col;

//assign zero to every array element

for(row=0;row<size;row++)

for(col=0;col<size;col++)  PascalTr[row,col]=0;

//first and second rows are set to 1s

PascalTr[0,0]=1;

PascalTr[1,0]=1;

PascalTr[1,1]=1;

for(row=2;row<size;row++){

PascalTr[row,0]=1;

for(col=1;col<=row;col++){

PascalTr[row,col]=PascalTr[row-1,col-1]+PascalTr[row-1,col];

}

}

//display the Pascal Triangle

for(row=0;row<size;row++){

for(col=0;col<=row;col++){

Console.Write("{0}\t",PascalTr[row,col]);

}

Console.WriteLine();

}

}

}

}

Comments

 Xinyaousing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CsharpExercise { class Array { static void Main(string[] args) { Console.WriteLine("Print matrix by size: "); int num = Convert.ToInt16(Console.ReadLine()); int[,] Darr = new int[num, num]; for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { if (j > i) Darr[i,j] = 0; else if (j == i || j == 0) Darr[i, j] = 1; else Darr[i, j] = Darr[i-1,j] + Darr[i, j-1]; } } for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { if (j > i) Console.Write(" "); else if (j == i || j == 0) Console.Write("1 "); else Console.Write(Darr[i, j] + " "); } Console.WriteLine(); } Console.ReadKey(); } } } 2017-07-13 Udayusing System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[,] arr = new int[6, 6]; for (int i = 0; i < 6; i++) { arr[i, 0] = 1; for (int j = 0; j <= i; j++) { if (j > 0) arr[i, j] = arr[i - 1, j] + arr[i - 1, j - 1]; Console.Write("{0,-10}", arr[i, j]); } Console.WriteLine(); } } } //Go for This Program, Its Simple2015-12-07 Monu SharmaYou can use the listed program to better understanding to print pascal triangle using two-dimensional array in c#. private void PrintPascalTrianlge() { System.Console.Write("Enter the number of rows: "); string number = System.Console.ReadLine(); int n = Convert.ToInt32(number); int[,] arr = new int[n, n]; // An auxiliary array to store generated pscal triangle values // Iterate through every line and print integer(s) in it for (int line = 0; line < n; line++) { // Every line has number of integers equal to line number for (int i = 0; i <= line; i++) { // First and last values in every row are 1 if (line == i || i == 0) arr[line, i] = 1; else // Other values are sum of values just above and left of above arr[line, i] = arr[line - 1, i - 1] + arr[line - 1, i]; System.Console.Write("{0:D} ", arr[line, i]); } System.Console.WriteLine(); } }2014-08-28

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.