# Thread: Two triangles of stars next to each other

1. Member Join Date
Sep 2011
Posts
14
Rep Power
0

## Two triangles of stars next to each other

All right, I have a problem with printing the next drawing:

Java Code:
```*
* *
* * *
* * * *
* * * * *
* * * * * * (x2)```
Two triangles (height 6 and width 6) next to each other and a space between every asterix.

I know the code for the first triangle and I know I have to use if and else to print the blanks, the problem is I don't know how to write it down propely.

Java Code:
```public class Triangles
{
public static void main(String[] args)
{
for (int i = 1; i <=6; i++)                         //for loop repeats 6 times
{
for (int j = 1; j<=i; j++)                        //while j <=i, j becomes j+1
{
System.out.print(" * " );                     //print asterix
}
System.out.println();                           //print result is the first triangle
}
}
}```
Now I know that for the first row (i) we have two columns with a asterix, namely the first one and the seventh one. The difference between them is 6 blanks. On the second row we have 4 asterixs and the difference becomes 5 blanks. So for every row we have, we get two asterix and we lose one blank until we have the last row with 12 asterix.

Any hints how to write this down using if and else inside the for loops?
Last edited by Aero; 09-24-2011 at 06:29 PM. Reason: Wrong look  Reply With Quote

2. ## Re: Two triangles of stars next to each other

You could use your current code, but will need to add some spaces in between the triangles on each line. I would do something like so:

Java Code:
```      // this makes a new line
for (int i = 1; i <= 6; i++) {

// this prints triangle * for one row
for (int j = 1; j <= i; j++) {
System.out.print(" * ");
}

// print spaces.  How many -- you've got to figure out!
for ....  // code for you to add

// print stars for one row. You know how to do this already.
for ...  // code for you to figure out and add.

System.out.println();
}```

So get a pen and paper and figure out how many blanks you need and what should be the best parameters to use in the middle for loop. :)

A hint: In your middle loop, I would have it print the exact number of spaces as you currently print for your star print, i.e.,
Java Code:
`System.out.print("   "); // three spaces to take the place of space-star-space.`
If you do it this way, the math works out nice and pretty. You'll just need to print more of this when i is small and less when i is bigger, and that's what the for loop is for.
Last edited by Fubarable; 09-24-2011 at 06:41 PM.  Reply With Quote

3. Member Join Date
Sep 2011
Posts
14
Rep Power
0

## Re: Two triangles of stars next to each other

Java Code:
```public static void main(String[] args)
{
for (int i = 1; i<=6; i++)                          //makes a new row
{
for (int j = 1; j<=i; j++)                        //makes a new column
{
System.out.print(" * " );                    //print triangle 1
}
for (int s = 5; s>=i; s--)                      //makes spaces while there are more spaces then rows
{
System.out.print("   ");                     //print spaces
}
for (int j = 1; j<=i; j++)                      //makes a new column
{
System.out.print(" * ");                   //print triangle 2
}
System.out.println();                        //print de rows accordingly to the loops
}
}
}```
This works perfect. Thanks a lot! Though I wonder how this problem can be solved with if and else. Do you have to declare variables outside your loop and then after the first triangle is made, then compare it with the rows i, print spaces, else print a star?

In other words:
Java Code:
```int spaces = 5;
for (int i = 1; i<=6; i++)                          //makes a new row
{
for (int j = 1; j<=i; j++)                        //makes a new column
{
System.out.print(" * " );                    //print triangle 1
}
if (spaces >=i)                                   //more spaces than rows
{
System.out.print("    ");                    //print spaces
}
else
{
System.out.print(" * ");
}
System.out.println();```
Last edited by Fubarable; 09-24-2011 at 07:42 PM. Reason: quote tags changed to code tags  Reply With Quote

4. Member Join Date
Sep 2011
Posts
14
Rep Power
0

## Re: Two triangles of stars next to each other

This thread can be closed also.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•