Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# I need help with code, to create a pyramid with asterisk

scott daston
Greenhorn
Posts: 5
I am stuck on what code to use to create a pyramid of asterisks using for loops.....
What I come up with is half of the pyramid (looks left justify)
SOmeone please help!!!

marc weber
Sheriff
Posts: 11343
Welcome to JavaRanch!

Show us the code you have so far, and we'll help you work it out.

scott daston
Greenhorn
Posts: 5
This is what I have so far

for (int x=0; x<4; x++)
{
for (int y=0; y<=x; y++)
{
System.out.print ("*");
}
System.out.println();
}

scott daston
Greenhorn
Posts: 5

Ultimatly this is what its suppose to look like.

Thanks to anyone that can help me.

EDIT by mw: Added Code Tags to keep formatting.
[ September 28, 2007: Message edited by: marc weber ]

marc weber
Sheriff
Posts: 11343
To illustrate one way of approaching this, consider the following triangle.

Try describing the logic in English first. Then you will have a better idea of what you're trying to code. For example, row by row, the above triangle is...

3 spaces and 1 asterisk
2 spaces and 3 asterisks
1 space and 5 asterisks
0 spaces and 7 asterisks

If you're printing this row by row, you probably have a loop iterating each row. So it makes sense to try and relate what you're doing to the row number (a variable x).

When x is 0: 3 spaces, 1 asterisk
When x is 1: 2 spaces, 3 asterisks
When x is 2: 1 space, 5 asterisks
When x is 3: 0 spaces, 7 asterisks

So the question is: If you know x, how do you get the number of spaces and the number of asterisks?

scott daston
Greenhorn
Posts: 5
thats the thing i am stuck, I am not sure how to code that exactly...I know the logic of it but the coding is what i need.
please help

marc weber
Sheriff
Posts: 11343
Originally posted by scott daston:
...I know the logic of it but the coding is what i need...

You know what the result should be, but do you know the logic detailed enough to code it?

For example, how can you express the number of spaces in terms of x? And how can you express the number of asterisks in terms of x?

scott daston
Greenhorn
Posts: 5
the thing is that I have been looking at this for hours and I can't seem to figure it out. Thanks for your help anyways!

marc weber
Sheriff
Posts: 11343
When x is 0: 3 spaces, 1 asterisk
When x is 1: 2 spaces, 3 asterisks
When x is 2: 1 space, 5 asterisks
When x is 3: 0 spaces, 7 asterisks

Notice that the number of leading spaces is always (3 - x).

Now if you can also see how the number of asterisks relates to x, then you will have a good idea of what to code for each row.

swimdudea swimdudeb
Greenhorn
Posts: 1
This is for people who will be searching for this same exact problem next semester for Intro to Java. I found nothing useful when browsing sites like this that explained or showed you the code. DO NOT COPY THIS CODE AS IT IS COPYRIGHT INFRINGEMENT...THIS IS MY CODE!

[DELETED]

Henry Wong
author
Marshal
Posts: 21405
84
swimdudea swimdudeb wrote:This is for people who will be searching for this same exact problem next semester for Intro to Java. I found nothing useful when browsing sites like this that explained or showed you the code. DO NOT COPY THIS CODE AS IT IS COPYRIGHT INFRINGEMENT...THIS IS MY CODE!

And there is a good reason for that. When a solution to a homework problem is posted -- people tend to not do the homework. And when people don't do homework, they don't learn.

Please help people learn. Thanks.

Henry

Siva Arun
Greenhorn
Posts: 28
Before start coding, always try to split the actions like below. (-) represents spaces here. This will speed up the coding and avoid confusions.

Maneesh Godbole
Saloon Keeper
Posts: 11092
13
swimdudea swimdudeb wrote:

Please check your private messages for an important administrative matter