Sunday 8 February 2015

Number pattern in C# - 26

Question: Program to print the binary Right triangle.

1
1 0
1 0 1
1 0 1 0
1 0 1 0 1

Code Snippet:


  1. using System;  
  2. public class Pattern  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int row, col, n;  
  7.   
  8.         Console.Write("Enter the no. of row: ");  
  9.         n = Convert.ToInt32(Console.ReadLine());  
  10.   
  11.         for(row = 1; row <= n; row++)  
  12.         {  
  13.             for(col = 1; col <= row; col++)  
  14.             {  
  15.                 if(col%2 == 0)  
  16.                 Console.Write("0 ");  
  17.                   
  18.                 else   
  19.                 Console.Write("1 ");  
  20.             }  
  21.             Console.WriteLine();  
  22.         }  
  23.   
  24.             Console.ReadLine();  
  25.     }  
  26. }

Output of number pattern
Fig: Screenshot of Output