1/*
2C# Program to Print a Binary Triangle
3This is a C# Program to Print a binary triangle.
4
5Problem Description
6This C# Program Prints a Binary Triangle.
7
8Problem Solution
9Binary Triangle is a Triangle formed with 1’s and 0’s.Number of rows in the binary triangle is obtained from the user.
10*/
11using System;
12using System.Collections.Generic;
13using System.Linq;
14using System.Text;
15
16namespace Lession5
17{
18 class Program
19 {
20 public static void Main(String[] args)
21 {
22 // Display Binary triangle
23 BinaryTriangle();
24
25 // Display Binary triable with formated
26 BinaryTriangleFormated();
27 }
28
29 private static void BinaryTriangle()
30 {
31 int p, lastInt = 0, input;
32 Console.Write("Enter the Number of Rows : ");
33 input = int.Parse(Console.ReadLine());
34 for (int i = 1; i <= input; i++)
35 {
36 for (p = 1; p <= i; p++)
37 {
38 if (lastInt == 1)
39 {
40 Console.Write("0");
41 lastInt = 0;
42 }
43 else if (lastInt == 0)
44 {
45 Console.Write("1");
46 lastInt = 1;
47 }
48 }
49 Console.Write("\n");
50 }
51 Console.ReadLine();
52 }
53
54 private static void BinaryTriangleFormated()
55 {
56 int i, j, k, numOfLines;
57 Console.Write("Enter the number of lines:");
58 numOfLines = Convert.ToInt32(Console.ReadLine());
59 for (i = 1; i <= numOfLines; i++)
60 {
61 for (k = numOfLines - i; k >= 1; k--)
62 {
63 Console.Write(" ");
64 }
65
66 if (i % 2 != 0)
67 {
68 for (j = 1; j <= i; j++)
69 {
70 if (j % 2 == 0)
71 {
72 Console.Write("0 ");
73 }
74 else
75 {
76 Console.Write("1 ");
77 }
78 }
79 }
80 else if (i % 2 == 0)
81 {
82 for (j = 1; j <= i; j++)
83 {
84 if (j % 2 == 0)
85 {
86 Console.Write("1 ");
87 }
88 else
89 {
90 Console.Write("0 ");
91 }
92 }
93 }
94 Console.WriteLine();
95 }
96 Console.ReadKey();
97 }
98 }
99}