Print the following patterns using loop :
b.
*
***
*****
***
*
The following code prints the pattern. I have added comments for each block to help you understand its functionality.
class Diamond
{
public static void main(String[] args)
{
int row = 3; // row with the maximum stars
// printing rows 1-3
for (int i=1; i<=row; i++)
{
// printing spaces for rows 1-3
for (int j=row; j>i; j--)
{
System.out.print(" ");
}
// printing stars for rows 1-3
for (int j=1; j<=((i*2)-1); j++)
{
System.out.print("*");
}
System.out.println();
}
// printing rows 4,5
for (int i=row-1; i>=1; i--)
{
// printing spaces for rows 4,5
for (int j=row-1; j>=i; j--)
{
System.out.print(" ");
}
// printing stars for rows 4,5
for (int j=1; j<=((i*2)-1); j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Here's a simple Python code snippet to print the specified pattern:
for i in range(1, 4):
print(" " * (3 - i) + "*" * (2 * i - 1))
for i in range(2, 0, -1):
print(" " * (3 - i) + "*" * (2 * i - 1))
.
affordable plumbing services in Bridgeport CT