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



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


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