Article

Create Pascal’s Triangle in C# and vb.net

In this article we will learn Create Pascal’s Triangle in C# and vb.net.

Create Pascal’s Triangle in C# and vb.net

The rows of Pascal’s triangle are conventionally enumerated starting with row n=0 at the top (the 0th row). The entries in each row are numbered from the left beginning with k=0 and are usually staggered relative to the numbers in the adjacent rows.

Code for create Pascal’s Triangle in C#

using System;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int rowNumber, c = 1, blank, i, j;

            Console.Write("\n\n");
            Console.Write("Input number of rows for Display the Pascal's triangle: ");
            Console.Write("\n\n");
            rowNumber = Convert.ToInt32(Console.ReadLine());
            for (i = 0; i < rowNumber; i++)
            {
                for (blank = 1; blank <= rowNumber - i; blank++)
                    Console.Write("  ");
                for (j = 0; j <= i; j++)
                {
                    if (j == 0 || i == 0)
                        c = 1;
                    else
                        c = c * (i - j  + 1) / j;
                    Console.Write("{0}   ", c);
                }
                Console.Write("\n");
               
            }
            Console.ReadLine();
        }
    }
}

Advertisement

Output for create Pascal’s Triangle in C#

create-pascals-triangle-in-c-and-vb-net

Code for create Pascal’s Triangle in vb.net

Imports System

Module Program
    Sub Main(args As String())
        Dim rowNumber, blank, i, j As Integer, c As Integer = 1

        Console.Write(vbLf & vbLf)
        Console.Write("Input number of rows for Display the Pascal's triangle: ")
        Console.Write(vbLf & vbLf)
        rowNumber = Convert.ToInt32(Console.ReadLine())

        For i = 0 To rowNumber - 1

            For blank = 1 To rowNumber - i
                Console.Write("  ")
            Next

            For j = 0 To i

                If j = 0 OrElse i = 0 Then
                    c = 1
                Else
                    c = c * (i - j + 1) / j
                End If

                Console.Write("{0}   ", c)
            Next

            Console.Write(vbLf)
        Next

        Console.ReadLine()

    End Sub
End Module

Output for create Pascal’s Triangle in vb.net

create-pascals-triangle-in-c-and-vb-net

Advertisement

Leave a Reply

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