Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Need Hint for Triangle???

1. ## Need Hint for Triangle???

Hi,

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

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

2. ## 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.

3. ## Re: Need Hint for Triangle???

Then show what you've tried. Ask specific questions, post sample runs, errors, etc. Then we can give hints.

4. ## 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

5. ## Re: Need Hint for Triangle???

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.

6. ## Re: Need Hint for Triangle???

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