Java Star Pattern 6
Photo Credit to CodeToFun
Java Star Pattern 6
Here`s a program that prints the above star pattern using Java Programming:
public class Demo
{
public static void main(String[] args)
{
int i, j, k;
for(i=5; i>=1; i--)
{
for(j=i; j<5; j++)
System.out.print(" ");
for(k=i*2; k>1; k--)
System.out.print("*");
System.out.println();
}
}
}
💻 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:
- public class Demo: This line declares a class named Demo which contains the main method where the program starts its execution.
- public static void main(String[] args): This is the main method where the program execution begins. It takes an array of strings as arguments (args), although in this program, args is not used.
- Inside the main method, three integer variables are declared: i, j, and k without initialization.
- for(i=5; i>=1; i--): This is an outer loop that iterates from i=5 to i=1. It decrements i by 1 in each iteration. This loop controls the number of rows in the pattern.
- for(j=i; j<5; j++): This is an inner loop that iterates from the current value of i to j=4. It prints spaces (" ") in each iteration, effectively creating leading spaces for each row.
- for(k=i*2; k>1; k--): This is another inner loop that iterates from k equals to i*2 down to k=1. It prints asterisks (*) in each iteration, effectively creating a sequence of asterisks.
- System.out.println();: This line is used to print a newline character after the inner loops complete. This moves the output to the next line, starting a new row in the pattern.
- After both inner loops complete, the program prints a newline character, effectively creating a new row in the pattern.
- The outer loop continues until i reaches 1, and the pattern is built row by row with decreasing numbers of spaces and increasing numbers of asterisks.
💯 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 (Java Star Pattern 6), please comment here. I will help you immediately.