Sunday 1 February 2015

Number Pattern in C# - 19

Question: Program to print the following Pattern.

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


Code Snippet:

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

Output of number pattern
Fig: Screenshot of output