Wednesday 11 February 2015

Star Pattern - 8

Question: Program to print the following Star Pattern.

* * * * * * * * *
   * * * * * * *
      * * * * *
         * * *
            *
         * * *
      * * * * *
   * * * * * * *
* * * * * * * * *

Code Snippet:

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

Output of Program
Fig: Screenshot of output