Tuesday, 9 December 2014

Star Pattern in C# - 7

Ques: Wap a program to print the given star pattern

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

Source Code:


  • using System;  
  • public class Pattern  
  • {  
  •     public static void Main()  
  •     {  
  •         int i,j,k,row;  
  •           
  •         Console.Write("Enter the no. of row: ");  
  •         row = Convert.ToInt32( Console.ReadLine() );  
  •   
  •         for(i=1; i<=row; i++)  
  •         {  
  •             for(j=row-i; j>=1; j--)  
  •             Console.Write(" ");  
  •   
  •             for(j=1; j<=i; j++)  
  •             Console.Write("*");  
  •   
  •             for(k=i-1; k>=1; k--)  
  •             Console.Write("*");  
  •             Console.WriteLine();  
  •         }  
  •   
  •         for(i=row-1; i>=1; i--)  
  •         {  
  •             for(k=row-i; k>=1; k--)  
  •             Console.Write(" ");  
  •           
  •             for(k=1; k<=(2*i-1); k++)  
  •             Console.Write("*");  
  •             Console.WriteLine();  
  •         }     
  •     }  
  • }

  • screenshot of star pattern program in c-sharp
    Fig: Screenshot of Output

    Note: We can use the logic of print second pattern in Line - 29 in both the Outer for loop.