* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
public class StarPattern17
{
public static void main(String[] args)
{
int n = 5;
// upper half
for(int i = 1; i <= n; i++)
{
// star - i
for(int j = 1; j <= i; j++)
{
System.out.print("*");
}
//Spaces - 2 * (n - i)
for(int j = 1; j <= 2*(n-i); j++)
{
System.out.print(" ");
}
//Stars - i
for(int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
// lower half
for(int i = n - 1; i >= 1; i--)
{
// star - i
for(int j = 1; j <= i; j++)
{
System.out.print("*");
}
//Spaces - 2 * (n - i)
for(int j = 1; j <= 2*(n-i); j++)
{
System.out.print(" ");
}
//Stars - i
for(int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.