C# Basic
C# Alphabet Pattern Programs
- C# Alphabet Pattern
- C# Alphabet Pattern 1
- C# Alphabet Pattern 2
- C# Alphabet Pattern 3
- C# Alphabet Pattern 4
- C# Alphabet Pattern 5
- C# Alphabet Pattern 6
- C# Alphabet Pattern 7
- C# Alphabet Pattern 8
- C# Alphabet Pattern 9
- C# Alphabet Pattern 10
- C# Alphabet Pattern 11
- C# Alphabet Pattern 12
- C# Alphabet Pattern 13
- C# Alphabet Pattern 14
- C# Alphabet Pattern 15
- C# Alphabet Pattern 16
- C# Alphabet Pattern 17
- C# Alphabet Pattern 18
- C# Alphabet Pattern 19
- C# Alphabet Pattern 20
- C# Alphabet Pattern 21
- C# Alphabet Pattern 22
- C# Alphabet Pattern 23
- C# Alphabet Pattern 24
- C# Alphabet Pattern 25
- C# Alphabet Pattern 26
- C# Alphabet Pattern 27
- C# Alphabet Pattern 28
- C# Alphabet Pattern 29
- C# Alphabet Pattern 30
- C# Alphabet Pattern 31
- C# Alphabet Pattern 32
- C# Alphabet Pattern 33
- C# Alphabet Pattern 34
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:
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:
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:
- 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.
- The program is contained within a namespace called myApp. Namespaces are used to organize code and prevent naming conflicts.
- The program contains a class named Program. In C#, the entry point for any program is the Main method in the class called Program.
- Inside the Main method, two variables i and j of type int are declared. These variables will be used in the loops later on.
- 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.
- 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.
- 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.
- 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.
- After printing a row, the program moves to a new line using Console.WriteLine() to start the next row.
- 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.
- The structure inside the second loop is the same as the first one, so the pattern is printed symmetrically.
- 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:
Author
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
If you have any doubts regarding this article (C# Alphabet Pattern 21) please comment here. I will help you immediately.