Saturday, 8 April 2017

C# Program to Print a BinaryTriangle

  1. /*
  2.  * C# Program to Print a BinaryTriangle
  3.  */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. namespace Program
  10. {
  11.     class Program
  12.     {
  13.         public static void Main(String[] args)
  14.         {
  15.             int p, lastInt = 0, input;
  16.             Console.WriteLine("Enter the Number of Rows : ");
  17.             input = int.Parse(Console.ReadLine());
  18.             for (int i = 1; i <= input; i++)
  19.             {
  20.                 for (p = 1; p <= i; p++)
  21.                 {
  22.                     if (lastInt == 1)
  23.                     {
  24.                         Console.Write("0");
  25.                         lastInt = 0;
  26.                     }
  27.                     else if (lastInt == 0)
  28.                     {
  29.                         Console.Write("1");
  30.                         lastInt = 1;
  31.                     }
  32.                 } Console.Write("\n");
  33.             }
  34.             Console.ReadLine();
  35.         }
  36.     }
  37. }
Here is the output of the C# Program:
Enter the Number of Rows : 5
1
01
010
1010
10101

No comments:

Post a Comment