Sunday 1 February 2015

Number Pattern in C# - 22

Question: Program to print the following Pattern.

          1
       2 2 2
    3 3 3 3 3
 4 4 4 4 4 4 4

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<=n-row; col++)  
  14.             Console.Write("  ");  
  15.   
  16.             for(col=1; col<=row; col++)  
  17.             Console.Write("{0} ", row);  
  18.   
  19.             for(col=1;col<row; col++)  
  20.             Console.Write("{0} ", row);  
  21.   
  22.             Console.WriteLine();  
  23.         }  
  24.   
  25.             Console.ReadLine();  
  26.           
  27.     }  
  28. }  

Output of number pattern
Fig: Screenshot of the output