C# PROGRAM TO PRINT HEART STAR PATTERN WITH NAME IN CENTER

using System;
public class HeartWithNameProgram
{
    public static void Main(string[] args)
    {
        int i, j, rows;
        string name = string.Empty;
        Console.Write("Enter your name: ");
        name = Convert.ToString(Console.ReadLine());
        int length = name.Length;
        Console.Write("Enter number of rows: ");
        rows = Convert.ToInt32(Console.ReadLine());

        // Loop to print upper part
        for (i = rows / 2; i <= rows; i += 2)
        {

            for (j = 1; j < rows - i; j += 2)
            {
                Console.Write(" ");
            }

            for (j = 1; j <= i; j++)
            {
                Console.Write("*");
            }

            for (j = 1; j <= rows - i; j++)
            {
                Console.Write(" ");
            }

            for (j = 1; j <= i; j++)
            {
                Console.Write("*");
            }

            Console.WriteLine();
        }

        // Loop to print lower part
        for (i = rows; i >= 1; i--)
        {
            for (j = i; j < rows; j++)
            {
                Console.Write(" ");
            }

            if (i == rows)
            {
                for (j = 1; j <= (rows * 2 - length) / 2; j++)
                {
                    Console.Write("*");
                }

                Console.Write( name);

                for (j = 1; j < (rows * 2 - length) / 2; j++)
                {
                    Console.Write("*");
                }
            }
            else
            {
                for (j = 1; j <= (i * 2) - 1; j++)
                {
                    Console.Write("*");
                }
            }

            Console.WriteLine();
        }  
        Console.Read();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *