Reputation: 33
I'm supposed to create some pattern - somewhat triangular - using for loop based on given number n.
For example, if given number n is 3, the pattern should be something like this :
**
*##*
*####*
And below is the piece of code I'm currently working on now.
public static void patterPrinters(int n) {
for (int k = 0; k < n; k++) {
for ( int x = n; x > k + 1; x--) {
System.out.print(" ");
}
for ( int z = n - k; z <= n; z++) {
System.out.print("**");
}
System.out.print("\n");
}
}
}
So far, I was able to make a similar shape, but of course, it is filled with stars (*) without the number signs(#) in between them. Like :
**
****
******
Could someone give me a hint as what I'm supposed to do from here?
Upvotes: 0
Views: 143
Reputation: 1
public static void patterPrinters(int n)
{
for (int k = 0; k < n; k++)
{
for (int x = n; x > k + 1; x--)
{
System.out.print(" ");
}
System.out.print("*");
for (int col = 0; col < k; col++)
{
System.out.print("##");
}
System.out.print("*\n");
}
}
Upvotes: 0
Reputation: 106
public static void patterPrinters(int n) {
int i,j,k;
for( i=0;i<n;i++)
{
for(k=0;k<((n-1)-i);k++)
{
System.out.print(" ");
}
System.out.print("*");
for(j=0;j<(i*2);j++)
{
System.out.print("#")
}
System.out.print("*\n");
}
}
check this out. All the Best.
Upvotes: 2
Reputation: 393771
HINT:
If you managed to print the correct shape, but with *
only, you can easily fix this by printing a single *
at the start and end of each row, and between them print x-2 #
s, where x is the number of *
you currently print in each row.
Upvotes: 0