Java Star Pattern 9
Photo Credit to CodeToFun
Java Star Pattern 9
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;
// First Part
for(i=1; i<=5; i++)
{
for(j=5; j>=1; j--)
{
if(i == j)
System.out.print("*");
else
System.out.print(" ");
}
for(k=2; k<=5; k++)
{
if(i == k)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
// Second Part
for(i=4; i>=1; i--)
{
for(j=5; j>=1; j--)
{
if(i == j)
System.out.print("*");
else
System.out.print(" ");
}
for(k=2; k<=5; k++)
{
if(i == k)
System.out.print("*");
else
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:
- Class Declaration and Main Method:
- The program starts with the declaration of a public class named Demo.
- Inside the class, there is a main method, which is the entry point of the program. The main method takes an array of strings as an argument (the args parameter).
- Variable Declarations:
- Inside the main method, three integer variables are declared: i, j, and k. These variables will be used in the loops to control the pattern's structure.
- First Part:
- This part of the program prints the first half of the pattern. It consists of a nested loop structure.
- The outer loop (for(i=1; i<=5; i++)) runs from 1 to 5. It controls the number of rows in the pattern.
- Inside the outer loop, there are two inner loops:
- The first inner loop (for(j=5; j>=1; j--)) runs from 5 to 1. It prints spaces if i is not equal to j and an asterisk if i is equal to j. This inner loop controls the left half of each row.
- The second inner loop (for(k=2; k<=5; k++)) runs from 2 to 5. It prints spaces if i is not equal to k and an asterisk if i is equal to k. This inner loop controls the right half of each row.
- After both inner loops, there is a System.out.println(); statement, which moves to the next row.
- Second Part:
- This part of the program prints the second half of the pattern, which is essentially the mirror image of the first half.
- It starts with a new loop structure:
- The outer loop (for(i=4; i>=1; i--)) runs from 4 to 1. It controls the number of rows in the second half of the pattern.
- Inside this outer loop, there are the same two inner loops as in the first part:
- The first inner loop (for(j=5; j>=1; j--)) prints spaces or asterisks to control the left half of each row.
- The second inner loop (for(k=2; k<=5; k++)) prints spaces or asterisks to control the right half of each row.
- After both inner loops, there is a System.out.println(); statement to move to the next row.
- Pattern Output:
- The program executes both the first and second parts, which together create a symmetric pattern of asterisks and spaces.
💯 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 9), please comment here. I will help you immediately.