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 1
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.
Solution 2
Sure, here is a simple Python program that does exactly that:
# Prompt the user for an integer
height = int(input("Enter the height of the triangle: "))
# Loop through each row
for i in range(height):
# Print the appropriate number of spaces
for j in range(height - i - 1):
print(" ", end="")
# Print the appropriate number of stars
for k in range(2 * i + 1):
print("*", end="")
# Move to the next line
print()
This program works by first prompting the user for an integer. It then loops through each row of the triangle. For each row, it first prints the appropriate number of spaces, then the appropriate number of stars, and finally moves to the next line.
The number of spaces is equal to the height of the triangle minus the current row number minus one, 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
You are given a positive integer . Print a numerical triangle of height
Write a program to print symmetric Pascal's triangle of "*" 's of height "l" of odd length . If input "l" is even then your program will print "Invalid line number".
John is eager to create a simple program for gathering and presenting personal details. Write a program that prompts users to provide the initial letter of their name (a character), age (an integer), and height in meters (a floating-point number).After collecting this information, display it in a well-organized format.Input format :The input consists of three space-separated values, representing the first letter of the name (character), age (integer), and height in meters (floating point value), separated by spaces.Output format :The first line prints the initial of the name in the format "Initial: [initial]"The second line prints the age in the format "Age: [age] years"The third line prints the height of the person, rounded to two decimal places, in the format "Height: [height] meters"
For this solution make sure you change the formatting on your answer to "Preformatted". We will NOT be grading you on formatting, but it will make the grader's life easier.Copy the skeleton of code below into your answer. Then in the space indicated, add your own code to prompt the user for a number. If the number is greater than 0 it should print a triangle of that size as shown below. If the number is less than or equal to 0 it should print nothing. See the transcripts below for some sample runs of this code: Enter a number: 3******A second run of this program with different input might look like this:Enter a number: 5***************A third run of this program with a zero or negative input might look like this:Enter a number: -12Use the skeleton of the main method below to write your code.public static void main(String[] args) { Scanner input = new Scanner(System.in); // TODO: Your code below this line input.close();}
Write a C program to print the Pyramid with numbersWrite a program to print a pyramid of numbers separated by spaces for the given number of rows.At the time of execution, the program should print the message on the console as:Enter number of rows : For example, if the user gives the input as:Enter number of rows : 5then the program should print the result as:Enter number of rows : 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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.