Sunday 1 February 2015

Number pattern in C# - 20

Question: Program to print the following pattern.


1234554321
1234__4321
123____321
12______21
1________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.         int temp=n;  
  11.   
  12.         for(row=1; row<=n; row++)  
  13.         {  
  14.             for(col = 1; col<=temp; col++)  
  15.             Console.Write("{0}", col);  
  16.           
  17.   
  18.             for(col=1; col<row; col++)  
  19.             Console.Write("__");  
  20.   
  21.             for(col=temp; col>=1; col--)  
  22.             Console.Write("{0}",col);  
  23.             temp--;  
  24.   
  25.             Console.WriteLine();  
  26.               
  27.         }  
  28.   
  29.             Console.ReadLine();  
  30.     }  
  31. }  


Output of Number Pattern
Fig: Screenshot of Output