programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

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

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
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)

Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:
Welcome to JavaRanch!

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

scott daston
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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.

marc weber
Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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.

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
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]

author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

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.

Henry

Greenhorn
Posts: 28
• Number of slices to send:
Optional 'thank-you' note:
Before start coding, always try to split the actions like below. (-) represents spaces here. This will speed up the coding and avoid confusions.

Bartender
Posts: 11497
19
• Number of slices to send:
Optional 'thank-you' note:

swimdudea swimdudeb wrote:

 Consider Paul's rocket mass heater.