# Thread: nested for loop!

1. Member
Join Date
Oct 2013
Posts
10
Rep Power
0

## nested for loop!

im trying to print this figure !!! Is there anyone that can help me in how to get the missing lines in the right of the triangle? i went to youtube and some other websites, but i wasn't able to find one that shows how to do those lines in the right side of the triangle!!
-----1-----
----333----
---55555---
--7777777--
-999999999-

but i keep getting this !

-----1
----333
---55555
--7777777
-999999999

this is what i have so far!!!

public class Example {
public static void main (String[] args){

for (int i=1; i<=9; i=i+2)
{

for (int k=0; k < (5- i / 2); k++)
{
System.out.print("-");
}

for (int j=0; j<i; j++)
{
System.out.print(i+"");

}

System.out.println("");

}

}//end of main

}//end of class

2. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Re: nested for loop!

It'd be much easier for us to read if you put it in a code box.

I don't see why you couldn't just add another forloop after you print the numbers that is the same exact thing as your first forloop that prints the dashes. You print out the same number of dashes on both sides...

3. Member
Join Date
Oct 2013
Posts
10
Rep Power
0

## Re: nested for loop!

I didn't know that you could put them in a box code , sorry im new , i will next time!!!
and thank you so much for you help i added another for loop and it works perfectly !!!

4. Member
Join Date
Jan 2014
Posts
9
Rep Power
0

## Re: nested for loop!

alright there is an easy way to solve this.. just use your second for loop
1) copy your 2nd for loop

for (int k=0; k < (5- i / 2); k++)
{
System.out.print("-");
}

2)paste it after the third for loop but before the system.out.println
3) Run it

#### Posting Permissions

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