C# Lab Program Part 2
C# Lab Program Part 2
C# Lab Program Part 2
Program :-
using System;
class Program
{
static void Main()
{
// Division by Zero Exception
DivideByZeroExceptionExample();
5. Develop a C# program to generate and printPascal Triangle using Two Dimensional arrays.
Program :-
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 :-
Pascal's triangle
1
11
121
1331
14641
6. Develop a C# program to generate and print Floyds Triangle using Jagged arrays.
using System;
Program:-
class Program
{
static void Main()
{
Console.Write("Enter the number of rows for Floyd's Triangle: ");
if (int.TryParse(Console.ReadLine(), out int rows))
{
int[][] floydsTriangle = GenerateFloydsTriangle(rows);
Console.WriteLine("Floyd's Triangle:");
PrintFloydsTriangle(floydsTriangle);
}
else
{
Console.WriteLine("Invalid input. Please enter a valid number of rows.");
}
}
Output :-
Enter the number of rows for Floyd's Triangle: 10
Floyd's Triangle:
1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55