Write a program to prompt user for an integer and display a triangle of stars with the same height as the input provided by the user. For example:
Question
Write a program to prompt user for an integer and display a triangle of stars with the same height as the input provided by the user.
For example:
Solution
Here is a simple Python program that accomplishes this task:
# Prompt the user for an integer
height = int(input("Enter the height of the triangle: "))
# Loop through each row of the triangle
for i in range(height):
# Calculate the number of spaces and stars for this row
spaces = ' ' * (height - i - 1)
stars = '*' * (2 * i + 1)
# Print the row
print(spaces + stars)
This program works by looping through each row of the triangle. For each row, it calculates the number of spaces and stars needed, and then prints them. The number of spaces is equal to the height of the triangle minus the current row number (starting from 0), and the number of stars is equal to twice the current row number plus one. This ensures that the triangle is centered and has the correct number of stars on each row.
Similar Questions
Write a program to display a menu driven facility to find the greatest of two numbers,three numbers and four numbers
Write a program to calculate the area of circle, triangle, rectangle and square using function overloading.
Complete the star by filling them with as many equilateral triangles of side 1 cm as you can. Count the numberof triangles in each case.
What will be the output of following program?main( ){int k, num = 30 ;k = ( num > 5 ? ( num <= 10 ? 100 : 200 ) : 500 ) ;printf ( "\n%d", num ) ;}
The program must accept four integers a, b, c and d as input. The program must solve the equation b * c - (a + d) and print it as the output.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.