Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# Alphabet Pattern 34

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

Photo Credit to CodeToFun

C# Alphabet Pattern 34

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, k;
      char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
      for (i = 0; i <= 4; i++) {
        for (j = 4; j >= 0; j--) {
          if (i == j)
            Console.Write("{0}", alpha[j]);
          else
            Console.Write(" ");
        }
        for (k = 1; k <= 4; k++) {
          if (i == k)
            Console.Write("{0}", alpha[k]);
          else
            Console.Write(" ");
        }
        Console.WriteLine();
      }
      for (i = 3; i >= 0; i--) {
        for (j = 4; j >= 0; j--) {
          if (i == j)
            Console.Write("{0}", alpha[j]);
          else
            Console.Write(" ");
        }
        for (k = 1; k <= 4; k++) {
          if (i == k)
            Console.Write("{0}", alpha[k]);
          else
            Console.Write(" ");
        }
        Console.WriteLine();
      }
    }
  }
}

💻 Testing the Program

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

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

🧠 How the Program Works

Let's break down the logic behind the code:

  1. The program starts with the using System; statement, which allows the program to access classes and methods from the System namespace.
  2. The namespace myApp statement declares a new namespace called "myApp," which helps organize the code and prevents naming conflicts with other namespaces.
  3. Inside the namespace, there's a class called Program. This is the main class that contains the program's entry point, which is the Main method.
  4. The Main method is declared as static void Main(string[] args). It is the entry point of the C# program, and it takes an array of strings (args) as input parameters.
  5. Three integer variables are declared: i, j, and k. These will be used as loop counters in the following iterations.
  6. A character array alpha is created and initialized with the characters "ABCDEFGHIJKLMNOPQRSTUVWXYZ". This array will be used to print letters of the alphabet.
  7. The first for loop for(i=0; i<=4; i++) runs from i = 0 to i = 4 (inclusive), meaning it will have five iterations. This loop represents the rows of the pattern.
  8. Inside the first for loop, there are two nested for loops, one for each half of the row.
    1. The first nested loop for(j=4; j>=0; j--) runs from j = 4 to j = 0 (inclusive) and prints spaces if i is not equal to j. Otherwise, it prints the alphabet character at index j from the alpha array.
    2. The second nested loop for(k=1; k<=4; k++) runs from k = 1 to k = 4 (inclusive) and prints spaces if i is not equal to k. Otherwise, it prints the alphabet character at index k from the alpha array.
  9. After the nested loops, Console.WriteLine() is called to move to the next line, creating a new row for the pattern.
  10. Once the first half of the pattern is printed, a similar process is repeated for the second half. A new for loop for(i=3; i>=0; i--) is used to print the remaining rows (from the fourth to the first).
  11. Finally, the Main method ends, and the program execution completes.

💯 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
11 months ago

If you have any doubts regarding this article (C# Alphabet Pattern 34) 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