Make a program that asks for a width, and then prints a pyramid of that width. The pyramid must be of odd width, and if the user enters an even number the program should print "Please enter an odd number" and ask again. (It is ok if the program gives an error if the user enters a non-number). Example: Please enter the pyramid width 10 Please enter an odd number Please enter the pyramid width 14 Please enter an odd number Please enter the pyramid width 13 * *** ***** ******* ********* *********** ************* The first line should have 1 asterisk, the second 3, then 5, 7, 9 etc. The number of spaces before starting the asterisks is (n−i)/2 Where n is the width of the pyramid and i is the number of asterixes in the current line. Note that this is always a whole number, as n and i are odd and hence (n-i) is even.
Make a program that asks for a width, and then prints a pyramid of that width. The pyramid must be of odd width, and if the user enters an even number the program should print "Please enter an odd number" and ask again. (It is ok if the program gives an error if the user enters a non-number).
Example:
Please enter the pyramid width
10
Please enter an odd number
Please enter the pyramid width
14
Please enter an odd number
Please enter the pyramid width
13
*
***
*****
*******
*********
***********
*************
The first line should have 1 asterisk, the second 3, then 5, 7, 9 etc. The number of spaces before starting the asterisks is
(n−i)/2
Where n is the width of the pyramid and i is the number of asterixes in the current line. Note that this is always a whole number, as n and i are odd and hence (n-i) is even.
Step by step
Solved in 4 steps with 2 images