Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# Star Pattern 8

Posted in C# Tutorial
Updated on Jan 10, 2024
By Mari Selvan
👁️ 117 - Views
⏳ 4 mins
💬 1 Comment
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:

example.cs
Copied
Copy To Clipboard
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:

Output
*       *
 *     *
  *   *
   * *
    *

🧠 How the Program Works

Let's break down the logic behind the code:

  1. The program starts with the usual using System; statement, which allows the use of functionalities from the System namespace in C#.
  2. The program declares a new namespace called myApp. A namespace is used to organize code and avoid naming conflicts with other libraries or assemblies.
  3. 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.
  4. In the Main method, three integer variables are declared: >, j, and k.
  5. 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.
  6. 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.
  7. 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(" ").
  8. 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.
  9. 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(" ").
  10. 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.
  11. 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:

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# Star Pattern 8) 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