this is a while loop structure....

the output is this

.....*
....***
...*****
..*******
.*********
***********

supposed to be... this must be a perfect triangle...
the dots represents blank spaces
my code was this

public class stars{
public static void main(String args[]){
int stars=5, space=5;
while(int i=1; i<=stars; i+2){
while(int j=10;j>=spaces; j--){
System.out.print(" ");
}
System.out.print("*");
System.out.print("\n");
}
}
}

if there's some thing wrong with it kindly fix please...

Recommended Answers

All 2 Replies

I think all you need to do is rowNumber*2-3 then keep decreasing 2spaces for every row.

i.e.: row 6 is the one on top, row 1 is the one in the buttom.
the nth equation is:

row 6.. row*2 - 3 = 9 spaces
row 5.. row*2 - 3 = 7 spaces
row 4.. row*2 - 3 = 5 spaces
row 3.. row*2 - 3 = 3 spaces
row 2.. row*2 - 3 = 1 space
row 1.. row*1 - 2 = since its below 0 then no spaces.

hope you can figure out the code on your own =)!

.........*
.......***
.....*****
...*******
.*********
***********

Need 3 loops, not 2. One for rows, one for spaces, one for stars. I don't know syntax of while you used; but here is sample (not tested/compiled) code:

for (rowNum=0; rowNum < rowCount; i++ )
{
    int blankCnt = (rowCount - rowNum) * 2 -3; //thanks to roswell67

    for (int i=0; i < blankCnt; i++ )
    {
    	System.out.print(" ");
    }
    for (int i=0; i<rowNum ; i++)
    {
	System.out.print("*");
    }
}

please mark thread as solved if this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.