I don't seem to be able to find the answer to this-
I need to draw a simple triangle using for loops.
*
***
*****
*******
*********
I can make a half triangle, but I don't know how to add to my current loop to form a full triangle.
*
**
***
****
*****
for (int i=0; i<6; i++)
{
for (int j=0; j<i; j++)
{
System.out.print("*");
}
System.out.println("");
}
Thanks-
Try this one in Java
A fun, simple solution:
Homework question? Well you can modify your original 'right triangle' code to generate an inverted 'right triangle' with spaces So that'll be like
This lets you have a little more control and an easier time making it:
Just change
public static int biggestoddnum
's value to whatever odd number you want it to be, and thefor(int k...)
has been tested to work.First think of a solution without code. The idea is to print an odd number of *, increasing by line. Then center the * by using spaces. Knowing the max number of * in the last line, will give you the initial number of spaces to center the first *. Now write it in code.
Well, there will be two sequences
size-n
for spaces and(2*(n+1)) -1
for stars. Here you go.