93.0k views
5 votes
Write a program that asks the user to enter the size of a triangle (an integer from 1 to 50). Display the triangle by writing lines of asterisks. The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user. On the next line write one fewer asterisk and continue by decreasing the number of asterisks by 1 for each successive line until only one asterisk is displayed. (Hint: Use nested for loops; the outside loop controls the number of lines to write, and the inside loop controls the number of asterisks to display on a line.) For example, if the user enters 3, the output would be:_______.a. *b. **c. ***d. **e. *

User JayantS
by
5.1k points

1 Answer

3 votes

Answer:

Implemented using Python

n = int(input("Sides: "))

if(n>=1 and n <=50):

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

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

print('*',end='')

print("")

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

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

print('*',end='')

print("")

else:

print("Range must be within 1 and 50")

Step-by-step explanation:

This line prompts user for number of sides

n = int(input("Sides: "))

The line validates user input for 1 to 50

if(n>=1 and n <=50):

The following iteration uses nested loop to print * in ascending order

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

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

print('*',end='')

print("")

The following iteration uses nested loop to print * in descending order

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

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

print('*',end='')

print("")

The following is executed if user input is outside 1 and 50

else:

print("Range must be within 1 and 50")

User Pasztorpisti
by
6.4k points