Saturday 7 February 2015

Number Pattern in C# - 25

Question: Program to print the following Number Pattern?


01
02 04
03 06 09
04 08 12 16
05 10 15 20 25

Code Snippet:


  1. using System;  
  2. public class Pattern  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int row, col, n, pr;  
  7.         Console.Write("Enter the no. of row: ");  
  8.         n = Convert.ToInt32(Console.ReadLine());  
  9.   
  10.         for(row = 1; row <= n; row++)  
  11.         {  
  12.             pr = 0;  
  13.             for(col = 1; col <= row; col++)  
  14.             {     
  15.                 pr = row*col;  
  16.       
  17.                 Console.Write("{0:D2} ", pr); // convert 1 to 01
  18.             }  
  19.             Console.WriteLine();  
  20.         }  
  21.             Console.ReadLine();  
  22.     }  
  23. }  

Output of number pattern in c-sharp.
Fig: Screenshot of output