C# Star Pattern 8
Photo Credit to CodeToFun
C# Star Pattern 8
Here`s a program that prints the above star pattern using C# Programming:
using System;
namespace myApp {
class Program {
static void Main(string[] args) {
int i, j, k;
for (i = 5; i >= 1; i--) {
for (j = 5; j >= 1; j--) {
if (i == j)
Console.Write("*");
else
Console.Write(" ");
}
for (k = 2; k <= 5; k++) {
if (i == k)
Console.Write("*");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}
💻 Testing the Program
When you run the above program, it will print the following output:
* * * * * * * * *
🧠 How the Program Works
Let's break down the logic behind the code:
- The program starts with the usual using System; statement, which allows the use of functionalities from the System namespace in C#.
- The program declares a new namespace called myApp. A namespace is used to organize code and avoid naming conflicts with other libraries or assemblies.
- Inside the myApp namespace, there's a class called Program, which is the entry point of the program. The Main method is the starting point of execution in a C# console application.
- In the Main method, three integer variables are declared: >, j, and k.
- The program enters a nested for loop. The outer loop starts with i initialized to 5, and it continues as long as i is greater than or equal to 1. The loop decrements i by 1 in each iteration.
- Inside the outer loop, there are two inner for loops. The first inner loop starts with j initialized to 5, and it continues as long as j is greater than or equal to 1. The loop decrements j by 1 in each iteration.
- Inside the first inner loop, there is an if-else statement. It checks if i is equal to j. If they are equal, it prints a * character using Console.Write("*"), indicating a diagonal line from the top-left corner to the bottom-right corner. Otherwise, it prints a space character using Console.Write(" ").
- After the first inner loop, there is a second inner loop starting with k initialized to 2, and it continues as long as k is less than or equal to 5. The loop increments k by 1 in each iteration.
- Inside the second inner loop, there is another if-else statement. It checks if i is equal to k. If they are equal, it prints a * character using Console.Write("*"), indicating a diagonal line from the top-right corner to the bottom-left corner. Otherwise, it prints a space character using Console.Write(" ").
- After both inner loops complete their iterations, the program writes a newline using Console.WriteLine(). This newline ensures that each row of the pattern is printed on a separate line.
- The outer loop continues to the next iteration until i becomes less than 1, and the pattern is fully printed.
💯 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 star 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# Star Pattern 8) please comment here. I will help you immediately.