Solved

# java triangle

Posted on 2011-03-01
738 Views
how is it possible to draw this type of triangle in java:

*
**
***
and

***
**
*
0
Question by:pauledwardian

LVL 47

Expert Comment

ID: 35014431
What do you mean by that question?
You cat just print like that:

System.out.println("  *");
System.out.println(" **");
System.out.println("***");

or you can create a panle and draw them and fill them in.

Please, explain what do you mean by this question?

0

LVL 92

Expert Comment

ID: 35014444
0

Author Comment

ID: 35014481
can you do these two triangles  by for loops please?

thanks,
edward
0

LVL 20

Expert Comment

ID: 35014812
put for loop !
and use system.out.print

for each(increment) it will print one *

put if  inside the loop
system.out.println

0

LVL 3

Accepted Solution

rmonsen earned 500 total points
ID: 35015854
``````		int trisize = 3;

// Draw first triangle
for (int i = 1; i <= trisize; i++) {
// Spaces infront
for(int j = 0; j < trisize - i; j++ ) {
System.out.print(" ");
}
// stars
for(int j = trisize; j > trisize - i; j-- ) {
System.out.print("*");
}
System.out.println();
}

// Draw second triangle
for (int i = trisize; i >= 1; i--) {
// Spaces infront
for(int j = 0; j < trisize - i; j++ ) {
System.out.print(" ");
}
// stars
for(int j = trisize; j > trisize - i; j-- ) {
System.out.print("*");
}
System.out.println();
}

// Draw both in one loop
for (int i = trisize; i >= -trisize; i--) {
int stars = 1 + trisize - Math.abs(i);
if (i == 0) continue; // Skip iteration

for(int j = 0; j < trisize - stars; j++ ) {
System.out.print(" ");
}
// stars
for(int j = trisize; j > trisize - stars; j-- ) {
System.out.print("*");
}
System.out.println();
}
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question