# nested for loop!

• 01-21-2014, 09:07 PM
Yoana
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
• 01-21-2014, 11:18 PM
AlexGraal
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...
• 01-21-2014, 11:44 PM
Yoana
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 !!!
• 01-23-2014, 07:57 PM
eddy20
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