Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# Star Pattern 9

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

Photo Credit to CodeToFun

C# Star Pattern 9

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 = 1; i <= 5; 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();
      }
      for (i = 4; 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 begins with the using System; statement, which allows the program to use classes and methods defined in the System namespace.
  2. The namespace myApp block defines a namespace called myApp. Namespaces help organize code and prevent naming conflicts.
  3. Inside the myApp namespace, there is a class called Program. The class Program block contains the program's main logic.
  4. The static void Main(string[] args) method is the entry point of the program. It is the first method that gets executed when the program starts running. The string[] args parameter allows the program to accept command-line arguments (if any).
  5. Inside the Main method, three integer variables i, j, and k are declared to be used later in the program.
  6. The program uses a nested loop structure to print a pattern of asterisks (*) and spaces.
  7. The outer loop for(i=1; i<=5; i++) iterates from i = 1 to i = 5. This loop is responsible for printing the first five rows of the pattern.
  8. Inside the outer loop, the first inner loop for(j=5; j>=1; j--) iterates from j = 5 to j = 1 in reverse order. This loop is responsible for printing spaces before the first asterisk in each row.
  9. The first if condition if(i == j) checks if the value of i is equal to the value of j. If they are equal, it means that the current position corresponds to the first asterisk in the row, so it prints an asterisk using Console.Write("*");.
  10. If the condition in the first if is not met, the else block will be executed, printing a space using Console.Write(" ");.
  11. After printing the spaces for the first part of the row, the second inner loop for(k=2; k<=5; k++) is used to print the remaining part of the row.
  12. The second if condition if(i == k) checks if the value of i is equal to the value of k. If they are equal, it means that the current position corresponds to the second asterisk in the row, so it prints an asterisk using Console.Write("*");.
  13. If the condition in the second if is not met, the else block will be executed, printing a space using Console.Write(" ");.
  14. After completing the row, Console.WriteLine(); is used to move to the next line, so the next row is printed on a new line.
  15. After the outer loop is finished (i.e., the first five rows are printed), there is another outer loop for(i=4; i>=1; i--) that iterates from i = 4 to i = 1. This loop is responsible for printing the last four rows of the pattern.
  16. The printing process in the second loop is similar to the first loop, with the only difference being that the order of printing asterisks and spaces is reversed.
  17. Once the entire pattern is printed, the program reaches the end of the Main method, 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 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
11 months ago

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