Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Printing a diamond pattern in C#

  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace ForgetCode
  5. {
  6. public class MainClass
  7. {
  8. public static void Main()
  9. {
  10. Console.WriteLine("Program for displaying pattern of *.");
  11. Console.Write("Enter the maximum number of *: (Top to Middle line)");
  12. int n = Convert.ToInt32(Console.ReadLine());
  13.  
  14. Console.WriteLine("\nHere is the Diamond of Stars\n");
  15.  
  16. for (int i = 1; i <= n; i++)
  17. {
  18. for (int j = 0; j < (n - i); j++)
  19. Console.Write(" ");
  20. for (int j = 1; j <= i; j++)
  21. Console.Write("*");
  22. for (int k = 1; k < i; k++)
  23. Console.Write("*");
  24. Console.WriteLine();
  25. }
  26.  
  27. for (int i = n - 1; i >= 1; i--)
  28. {
  29. for (int j = 0; j < (n - i); j++)
  30. Console.Write(" ");
  31. for (int j = 1; j <= i; j++)
  32. Console.Write("*");
  33. for (int k = 1; k < i; k++)
  34. Console.Write("*");
  35. Console.WriteLine();
  36. }
  37.  
  38. Console.WriteLine();
  39. }
  40. }
  41. }

Output:
  1. *
  2. ***
  3. *****
  4. *******
  5. *********
  6. *******
  7. *****
  8. ***
  9. *