Basic Programming C# Examples

C# Program to display by Pascal’s triangle

The Program in C# Program to display by Pascal’s triangle is given below:

using System;

namespace PascalTriangle
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number of rows to display Pascal's triangle: ");
            int rows = int.Parse(Console.ReadLine());

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    Console.Write(PascalNumber(i, j) + " ");
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }

        static int PascalNumber(int i, int j)
        {
            if (j == 0 || j == i)
            {
                return 1;
            }
            return PascalNumber(i - 1, j - 1) + PascalNumber(i - 1, j);
        }
    }
}

Output:

Hello Codeauri Family, enter the number of rows to display Pascal’s triangle: 7
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1

Pro-Tips💡

Here are the step by step execution of above program:

  1. The program starts by including the System namespace using the using keyword.
  2. The code is contained within a namespace called PascalTriangle. This allows the code to be organized and reused easily in other projects.
  3. The Program class contains the main method Main, which is the starting point of the program.
  4. Within the Main method, the user is prompted to enter the number of rows to display Pascal’s triangle using the Console.WriteLine method. The user’s input is then converted to an int using int.Parse and stored in the rows variable.
  5. The code uses two nested for loops to iterate over the rows and columns of Pascal’s triangle.
  6. The inner loop uses the PascalNumber method to calculate the value of each number in the triangle. The method is passed two arguments, the row number i and the column number j.
  7. The PascalNumber method uses recursion to calculate the value of each number in the triangle. If j is 0 or equal to i, the method returns 1. This is because the first and last number in each row is always 1.
  8. If j is not 0 or equal to i, the method calculates the value of the number by adding the value of the number in the previous row at position j - 1 and the number in the previous row at position j.
  9. The Main method writes each value of the triangle on a new line using the Console.Write method. After each row, the method writes a newline using the Console.WriteLine method to create a triangular shape.
  10. The Console.ReadLine method is used to pause the program and wait for the user to press Enter.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X