w3resource

C#: Display the pattern like a diamond


C# Sharp For Loop: Exercise-31 with Solution

Write a program in C to display the pattern like a diamond.
The pattern is as follows :

      * 
     *** 
    ***** 
   *******
  *********
   *******
    *****
     ***
      *
C# Sharp: Display the pattern like a diamond

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise31  
{  
    public static void Main()
{
   int i,j,r;
   
	Console.Write("\n\n");
    Console.Write("Display the pattern like diamond:\n");
    Console.Write("-----------------------------------");
    Console.Write("\n\n");     
   
   Console.Write("Input number of rows (half of the diamond) :");
   r = Convert.ToInt32(Console.ReadLine());   
   for(i=0;i<=r;i++)
   {
     for(j=1;j<=r-i;j++)
     Console.Write(" ");
     for(j=1;j<=2*i-1;j++)
     Console.Write("*");
     Console.Write("\n");
   }
 
   for(i=r-1;i>=1;i--)
   {
     for(j=1;j<=r-i;j++)
     Console.Write(" ");
     for(j=1;j<=2*i-1;j++)
       Console.Write("*");
     Console.Write("\n");
   }
  }
}

Sample Output:

Display the pattern like diamond:                                                      
-----------------------------------
Input number of rows (half of the diamond) :8                                                                                                         
       *             
      ***                           
     *****                           
    *******                         
   *********                        
  ***********                        
 *************
***************                   
 *************                      
  ***********                     
   *********                        
    *******                  
     *****                              
      ***                
       *

Flowchart:

Flowchart : Display the pattern like diamond

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp Program to find the Armstrong number for a given range of number.
Next: Write a C# Sharp Program to determine whether a given number is prime or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.




We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook