C# Program to Print a Binary Triangle

Algorithm:-

(1)Start

(2)take two variable input,ninary with initial value zero

(3)compare the binary value to 1 if equal then print zero(0) and initialize binary value one(1) 

(4)compare the binary value to zero if equal then print one(1) and initialize binary value zero(1).

(5)Stop


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace TestApplicationOnly

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                Int binary= 0, input;

                Console.WriteLine("Enter the Number of Rows : ");

                input = int.Parse(Console.ReadLine());

                for (int i = 1; i <= input; i++)

                {

                    for (int p = 1; p <= i; p++)

                    {

                        if (binary == 1)

                        {

                            Console.Write("0");

                            binary = 0;

                        }

                        else if (binary == 0)

                        {

                            Console.Write("1");

                            binary = 1;

                        }

                    } Console.Write("\n");

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine("input is not in correct formate");

            }

            Console.ReadKey();  

        }

    }

}


Output:-

Enter the Number of Rows : 5

1

01

010

1010

10101

एक टिप्पणी भेजें

0 टिप्पणियाँ