Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# Alphabet Pattern 21

Posted in C# Tutorial
Updated on Jan 10, 2024
By Mari Selvan
👁️ 72 - Views
⏳ 4 mins
💬 1 Comment
C# Alphabet Pattern 21

Photo Credit to CodeToFun

C# Alphabet Pattern 21

Here`s a program that prints the above alphabet pattern using C# Programming:

example.cs
Copied
Copy To Clipboard
using System;

namespace myApp {
  class Program {
    static void Main(string[] args) {
      int i, j;
      char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
      for (i = 1; i <= 5; i++) {
        for (j = 1; j < i * 2; j++) {
          if (j % 2 == 0)
            Console.Write("*");
          else
            Console.Write(alpha[i - 1]);
        }
        Console.WriteLine();
      }
      for (i = 4; i >= 1; i--) {
        for (j = 1; j < i * 2; j++) {
          if (j % 2 == 0)
            Console.Write("*");
          else
            Console.Write(alpha[i - 1]);
        }
        Console.WriteLine();
      }
    }
  }
}

💻 Testing the Program

When you run the above program, it will print the following output:

Output
A
B*B
C*C*C
D*D*D*D
E*E*E*E*E
D*D*D*D
C*C*C
B*B
A

🧠 How the Program Works

Let's break down the logic behind the code:

  1. The using System; statement includes the System namespace, which contains fundamental classes and base classes that define commonly-used value and reference data types, events, and event handlers, interfaces, attributes, and processing exceptions. It allows the program to use functionalities defined in the System namespace.
  2. The program is contained within a namespace called myApp. Namespaces are used to organize code and prevent naming conflicts.
  3. The program contains a class named Program. In C#, the entry point for any program is the Main method in the class called Program.
  4. Inside the Main method, two variables i and j of type int are declared. These variables will be used in the loops later on.
  5. The character array alpha is initialized with the uppercase English letters from A to Z using the ToCharArray() method. It will be used to print the letters in the pattern.
  6. The first for loop is used to print the upper half of the pattern. It runs for i from 1 to 5, indicating the number of rows.
  7. The second for loop is used to print each row. It runs for j from 1 to i * 2, which creates the pattern's width based on the row number i.
  8. Inside the loop, an if-else statement is used to determine whether to print a letter or an asterisk. If the value of j is even (j % 2 == 0), it prints an asterisk; otherwise, it prints the letter at the index i-1 from the alpha array.
  9. After printing a row, the program moves to a new line using Console.WriteLine() to start the next row.
  10. Once the upper half of the pattern is printed, the program enters another for loop to print the lower half of the pattern. This loop runs for i from 4 to 1, creating the decreasing number of rows.
  11. The structure inside the second loop is the same as the first one, so the pattern is printed symmetrically.
  12. When the loops are finished executing, the Main method completes, and the program execution ends.

💯 Tips for Enhancement:

Explore the versatility of this pattern by adjusting its parameters. Whether you increase or decrease the size, tweak the spacing, or modify the characters used, each change opens up a world of possibilities, allowing you to customize and create your unique visual effects.

✔ Conclusion:

Creating visually appealing patterns is not only a fun endeavour but also a great way to enhance your programming or design skills. We hope this tutorial has inspired you to explore the world of creative coding. Share your creations with us, and let your imagination run wild!

🤗 Closing Call-to-Action:

We'd love to see your unique interpretations of the alphabet pattern. Share your creations in the comments below, and don't hesitate to reach out if you have any questions or suggestions for future tutorials. Happy coding!

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
1 year ago

If you have any doubts regarding this article (C# Alphabet Pattern 21) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy