Saturday 31 January 2015

Number Pattern in C# - 18

Question: Program to print the following pattern.


9
0 1
2 3 4
5 6 7 8
9 0 1 2 3


Code Snippet:

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


Output of pattern programming
Fig: Screenshots of Output