w3resource

C#: Display alphabet pattern like A with an asterisk

C# Sharp For Loop: Exercise-58 with Solution

Write a C# Sharp program to display an alphabet pattern like A with an asterisk.

Visual Presentation:

C# Sharp Exercises: Display alphabet pattern like A with an asterisk

Sample Solution:

C# Sharp Code:

using System;  // Importing necessary namespace

public class Exercise58  // Declaration of the Exercise58 class
{  
    public static void Main()  // Main method, entry point of the program
    {
        int row, column;  // Declaration of integer variables for rows and columns

        // Display a message about the pattern being created
        Console.Write("\n\n");
        Console.Write("Display the pattern like 'A' with an asterisk:\n");
        Console.Write("---------------------------------------------");
        Console.Write("\n\n");   

        // Loop for iterating through rows
        for (row = 0; row <= 7; row++)
        {
            // Loop for iterating through columns
            for (column = 0; column <= 7; column++)
            {
                // Check conditions to print '*' or ' ' for specific positions
                if (((column == 1 || column == 5) && row != 0) || 
                    ((row == 0 || row == 3) && (column > 1 && column < 5)))
                {
                    Console.Write("*");  // Print '*' for specific conditions
                }
                else
                {
                    Console.Write(" ");  // Print ' ' for other positions
                }
            }
            Console.Write("\n");  // Move to the next line after each row is printed
        }

        Console.Write("\n");  // Add an extra line at the end for better readability
    }
}

Sample Output:

Display the pattern like 'A' with an asterisk:                                                              
---------------------------------------------                                                                  
  ***                                                                                                   
 *   *                                                                                                   
 *   *                                                                                                   
 *****                                                                                                   
 *   *                                                                                                   
 *   *                                                                                                   
 *   *                                                                                                   
 *   *

Flowchart:

Flowchart: display alphabet pattern like A with an asterisk

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to print a string in reverse order.
Next: Write a C#Sharp program to display alphabet pattern like 'B' with an asterisk.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/csharp-exercises/for-loop/csharp-for-loop-exercise-58.php