Write a program that reads in an integer and displays a diamond pattern as described below. the integer determines the the number of plus characters in the widest (center) part of the diamond. the program should use nested loops. you can assume the number read in is odd.

Here is one sample run:


Diamond Width: 7

+

+++

+++++

+++++++

+++++

+++

+

Respuesta :

Answer:

The program in Python is as follows:

numrows = int(input())

n = 0

for i in range(1, numrows + 1):

   for j in range (1, (numrows - i) + 1):

       print(end = " ")

   while n != (2 * i - 1):

       print("*", end = "")

       n = n + 1

   n = 0

   print()

k = 1; n = 1

for i in range(1, numrows):

   for j in range (1, k + 1):

       print(end = " ")

   k = k + 1

   while n <= (2 * (numrows - i) - 1):

       print("*", end = "")

       n = n + 1

   n = 1

   print()

Explanation:

This gets the number of rows

numrows = int(input())

This initializes the upper part of the diamonds

n = 0

The following iteration prints the spaces in each row

for i in range(1, numrows + 1):

   for j in range (1, (numrows - i) + 1):

       print(end = " ")

The following iteration prints * in the upper part of the diamond

   while n != (2 * i - 1):

       print("*", end = "")

       n = n + 1

   n = 0

This prints a new line

   print()

The lower part of the diamond begins here

k = 1; n = 1

The following iterations print the spaces in each row

for i in range(1, numrows):

   for j in range (1, k + 1):

       print(end = " ")

   k = k + 1

The following iteration prints * in the lower part of the diamond

   while n <= (2 * (numrows - i) - 1):

       print("*", end = "")

       n = n + 1

   n = 1

This prints a new line

   print()

ACCESS MORE