For Loop Help!

Below is my class assignment. I have no idea how to get the correct number of stars into the for loops for any number given...Help, please?!?!

Write a program using for loops to produce the following output. Please read carefully the entire specification! Your program must work for any odd number entered by the user, not just 13.
Enter an odd number width: 13

Hill:
*
***
*****
*******
*********
***********
*************

Triangle:
*
***
*****
*******
*********
***********
*************

Diamond:
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
Note**The stars aren't showing up correctly above, but they are supposed to be in the configuration stated above each set of stars.
Last edited on
closed account (o3hC5Di1)
Hi there,

Let's break the assignment down:

Your program must work for any odd number entered by the user


So the user should be able to enter a number, and you should verify that the number is odd. You will need to:
- create a numerical-type variable
- ask the user for a number (ie print a question on the screen)
- store the entered number in the number-type variable
- check if the number is odd (hint: use the modulus operator, %), if it is not odd, then ask the user again.

Write a program using for loops to produce the following output.


So when you did the above, you have a numerical-type variable (let's call it user_number for now). You will then need to create a for loop which runs as many times as that user_number:

for (int i=0; i < user_number; i++)

As for the output, you will need to print two things to the screen: spaces and asterisks (*). You will start with a certain amount of spaces on each side of the asterisk, then, as the for-loop repeats, you will need to change the amount of spaces and asterisks until you have the desired shape.

That should get you started. Please do come back to us with any attempted code you have written if you need any further help.

All the best,
NwN
Topic archived. No new replies allowed.