Hi,
I need to build the triangle like below.Please give hint to solve this using loops.
Code Java:
* * * * * * * * * * * * * * *
thanks in advance.
Printable View
Hi,
I need to build the triangle like below.Please give hint to solve this using loops.
Code Java:
* * * * * * * * * * * * * * *
thanks in advance.
Hi, read this Announcements - What's Wrong With My Code? to find out how to post formatted code and to preserve formatting.
Then show what you've tried. Ask specific questions, post sample runs, errors, etc. Then we can give hints.
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.
Code :
public class MyTriangle{ public static void main(String args[]){ MyTrinagle mt = new MyTriangle(); // up to you .... } }