Java Pyramids

This is a short, classic pyramids of asterisks example which teaches some cool uses of for loops, and hopefully helps you to discover the potential of for loops. We're going to make a program that displays this:

*
**
***
****
*****
******
*******
********
*********

Creating the Program

First thing you're going to want to do is create a new Java class. You may call yours whatever you like, or you can use the same class name as in this example. Make sure that there is the standard main method inside of your new class.

Because this is a looping example, we're going to need a loop. More specifically, we're going to be using a for loop.

Notice how the pyramid has nine rows? That's exactly what we want our for loop to accomplish for us. So make a for loop that starts at 1 and goes all the way to 9. This is what it should look like:

Look at how my second condition is x < 10. This means I want x to be less than 10. When x is equal to 10 then the for loop should stop. This is the way you make it loop up to 9.

Now all we have to do is print out the asterisks. But wait a minute, we have to print out a different number of asterisks each time! How will we know how many asterisks to print out?

The answer lies again in looping. We have a loop that loops through the rows, but perhaps we also need a loop that loops through the number of asterisks. Notice how it starts with one star and goes all the way to 9? Well our for loop already almost takes care of this for us. Now we just need a loop that prints out one asterisk at a time, all the way up to whatever number we're at with our current for loop. Take a look at this:

First of all, I'm using a different variable in the inner for loop, because x is already being used for the outer for loop. Second of all, Notice the 2nd condition. It is saying to loop until you reach the value of x. This will ensure that we print as many asterisks as the row number that we are on. Row 1 prints out 1 star, row 2 prints out 2 stars, etc.

Now we just need to add all the print statements we need to get it to display:

Do you see how those print statements work? I'm not using println for the inner for loop because I want the asterisks to be printed out side by side. I also have an empty println under the inner for loop so that a new row is begun once a row of asterisks is printed out.

Backwards Pyramid

How can we modify the code we've written to create a backwards pyramid? You know, one that starts with 9 stars and then ends with 1?

We can actually do this by changing just one line of code.

Remember that the way stars are printed out is determined by the for loops we created. If we change the way those loops work, then we can print out these asterisks backwards. How do we do that?

First let's look at the outer for loop. It counts from 1 to 9. Wouldn't it make sense to just make it count from 9 to 1? Here's how you would do this:

Try running the program. The asterisks should be backwards now! You didn't even have to change the inner for loop. Why? Because the inner for loop prints out as many asterisks as what x is equal to. So since we flipped the way x works, we have flipped the way the asterisks are printed out.

The reason we can make the for loop go backwards lies in the 3rd condition, the counting condition. We are telling the for loop to subtract one from x each time it loops. We have replaced the two pluses with two minus signs.

So there you have it, a mini-example on nesting for loops together as well as making for loops count backwards. Feel free to experiment with your own asterisk combinations!

If you have any questions, comments, or concerns, feel free to contact us.


Need more help? Want to see sample programs to make many different kinds of Pyramids? Don't see the exact solution you're looking for? Sign up for the Fresh Cup of Java Newsletter today to receive Java tips, sample programs, answers to your questions, and much more!


Sign Up For Free Bonus Content!

Enter your E-mail Address

Enter your First Name (optional)

Then

Don't worry -- your e-mail address is totally secure.
I promise to use it only to send you Fresh Cup of Java.

Custom Search