PYTHON PROGRAM THAT ALLOWS THE USER TO SPECIFY HOW HIGH THE TRIANGLE SHOULD BE AND PRINTS USING FOR LOOP
Use a for loop to print a triangle like the one below. Allow the user to specify how high the triangle should be.
*
* *
* * *
* * * *
CODE:
n=int(input("Enter the height of the triangle:"))
for i in range(1,n+1):
for j in range(1,i+1):
print("*",end=" ")
print("\n")
EXPLANATION:
This program will ask the user to enter the height of the triangle.
According to the height of the triangle, using for loop it prints the triangle.
Outer for loop will iterate n times and inner for loop will iterate i times to print the mentioned triangle.
In the print statement present inside the loop, end=" " will print space after the print statement has been
executed.
In the last line of code "\n" is a line break function which is used to break line and to make next
sentences to compile in the new line(next line).
OUTPUT:
Comments
Post a Comment