Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# Alphabet Pattern 16

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

Photo Credit to CodeToFun

C# Alphabet Pattern 16

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

💻 Testing the Program

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

Output
    A
  B C D
E F G H I

🧠 How the Program Works

Let's break down the logic behind the code:

  1. The program starts by importing the necessary namespace System, which provides access to the Console class and other system-related functionalities.
  2. The program defines a class called Program within the namespace myApp.
  3. Inside the Program class, the Main method is declared, which is the entry point of the program. It takes an array of strings args as input, although it is not used in this program.
  4. Three variables are declared: i, j, and k. i and j are integers, and k is initialized to 0. These variables will be used for loop control and array indexing.
  5. An array of characters named alpha is created and initialized with the characters 'A' to 'Z'. This array contains all the uppercase letters of the English alphabet.
  6. The program enters a for loop with the variable i starting at 1 and incrementing by 2 in each iteration, and it runs as long as i is less than or equal to 5.
  7. Inside the outer loop, there is another for loop with the variable j starting at 5 and decrementing by 1 in each iteration, running as long as j is greater than or equal to 1.
  8. In each iteration of the inner loop, the program checks if j is greater than i. If j is greater than i, it means there should be spaces printed, so the program writes a space character to the console using Console.Write(" ").
  9. If j is not greater than i, it means it's time to print a character from the alpha array. The program writes the character at index k from the alpha array to the console using Console.Write(alpha[k++] +" ").
  10. After printing the characters or spaces for each row, the program moves to the next line using Console.WriteLine() to start a new row.
  11. The outer loop continues until i exceeds 5, and the pattern of characters and spaces is printed accordingly.
  12. Once the outer loop is finished, 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
9 months ago

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