Sunday 1 February 2015

Number Pattern in C# - 21

Question: Program to print the following pattern.

12344321
123321
1221
11

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 row: ");  
  9.         n=Convert.ToInt32(Console.ReadLine());  
  10.         int temp = n;  
  11.   
  12.         for(row=n; row>=1; row--)  
  13.         {  
  14.             for(col = 1; col<=temp; col++)  
  15.             Console.Write("{0} ", col);  
  16.   
  17.             for(col=temp; col>=1; col--)  
  18.             Console.Write("{0} ", col);  
  19.             temp--;  
  20.           
  21.             Console.WriteLine();  
  22.         }  
  23.   
  24.             Console.ReadLine();  
  25.     }  
  26. }  

Output of Number Pattern
Fig: Screenshot of Output