125k views
5 votes
Complete the program below that takes in one positive, odd, integer, n (at least 3), and prints a "diamond" shape of stars with the widest part of the diamond consisting of n stars. For example, if the input is n=5, the output would be:

*
***
*****
***
*
If the input is n=9, the output would be:
*
***
*****
*******
*********
*******
*****
***
*
The diamond shape has rows of stars (asterisks) with and odd number of stars on each row. In the n=9 example, the rows had 1 star, 3 stars, 5 stars, 7 stars, 9 stars, 7 stars, 5 stars, 3 stars, and 1 star. Additionally, the rows with less than n stars have leading spaces to "center" the row (so that the center star of each row is directly above/below the center star of the other rows). Spaces are important in this assignment. Assume there are no trailing spaces (spaces at the end of each row). Because n must be odd and each row will have an odd number of stars, every row will have a "center" star.

1 Answer

5 votes

Answer:

n = int(input("Enter the n (positive odd integer): "))

for i in range(1, n+1, 2):

print(i*"*")

for i in range(n-2, 0, -2):

print(i*"*")

Step-by-step explanation:

*The code is in Python.

Ask the user to enter the n

Create a for loop that iterates from 1 to n, incrementing by 2 in each iteration. Print the corresponding number of asterisks in each iteration

Create another for loop that iterates from n-2 to 1, decrementing by 2 in each iteration. Print the corresponding number of asterisks in each iteration

User Sandeep Fatangare
by
8.3k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.