# Need Hint for Triangle???

• January 16th, 2014, 05:37 AM
Ganeprog
Need Hint for Triangle???
Hi,

I need to build the triangle like below.Please give hint to solve this using loops.

Code Java:

``` * * * * * * * * * * * * * * *```

• January 16th, 2014, 06:24 AM
PhHein
Re: Need Hint for Triangle???
Hi, read this Announcements - What's Wrong With My Code? to find out how to post formatted code and to preserve formatting.
• January 16th, 2014, 06:46 AM
GregBrannon
Re: Need Hint for Triangle???
Then show what you've tried. Ask specific questions, post sample runs, errors, etc. Then we can give hints.
• January 17th, 2014, 01:10 AM
Ganeprog
Re: Need Hint for Triangle???
I tried but i dont know how to start this. give some hint or idea to solve this above triangle or Please provide some initiation point to start
• January 17th, 2014, 02:19 AM
andbin
Re: Need Hint for Triangle???
Quote:

Originally Posted by Ganeprog
I tried but i dont know how to start this. give some hint or idea to solve this above triangle or Please provide some initiation point to start

First, I imagine you want (or you have been asked to do) to have a variable that contains the "height" of the triangle, e.g.:

int height = 5;

And given only this value, generate dynamically the triangle, so that changing this value you get another correct triangle.

You need a main loop (for) cycle for each row, I think this is quite trivial. For each row you need to do another loop, some prints but also few calculations.
If you want to have a fixed number of spaces at the beginning of each line, it's ok, it's a simple fixed println. Then you need a number of spaces that depends on the current row. On the first row, how many spaces do you need before the single "*" in order to have this centered?
And you need a space between two consecutive "*".

So please, start from these considerations.
• January 17th, 2014, 02:20 AM
PhHein
Re: Need Hint for Triangle???
Code :

```public class MyTriangle{ public static void main(String args[]){ MyTrinagle mt = new MyTriangle(); // up to you .... } }```