Search For

Printing a diamond pattern

Flag Hide Line Nos
Flagging a Snippet, takes the snippet to Moderator Attention;


0
using System;
using System.Text.RegularExpressions;

namespace ForgetCode
{
    public class MainClass
    {
        public static void Main()
        {
            Console.WriteLine("Program for displaying pattern of *.");
            Console.Write("Enter the maximum number of *: (Top to Middle line)");
            int n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("\nHere is the Diamond of Stars\n");

            for (int i = 1; i <= n; i++)
            {
                for (int j = 0; j < (n - i); j++)
                    Console.Write(" ");
                for (int j = 1; j <= i; j++)
                    Console.Write("*");
                for (int k = 1; k < i; k++)
                    Console.Write("*");
                Console.WriteLine();
            }

            for (int i = n - 1; i >= 1; i--)
            {
                for (int j = 0; j < (n - i); j++)
                    Console.Write(" ");
                for (int j = 1; j <= i; j++)
                    Console.Write("*");
                for (int k = 1; k < i; k++)
                    Console.Write("*");
                Console.WriteLine();
            }

            Console.WriteLine();
        }
               
    }
}

Output:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
Vote! You feel this code is working like charm ? Go a head Login and Press up arrow.
Didn't work ?, press down arrow and let us know the error in comments

Comments