C# Sharp Exercises: 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 :
* *** ***** ******* ********* ******* ***** *** *
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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework