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

Popular posts from this blog

PYTHON PROGRAM THAT USES FOR LOOP TO PRINT THE NUMBERS 8,11,14,17,20,.........,83,86,89

PYTHON PROGRAM THAT ASKS THE USER TO ENTER TWO STRINGS OF SAME LENGTH AND WILL ALTERNATE THE CHARACTERS OF THE TWO STRINGS.FOR EXAMPLE: abcde and ABCDE THEN THE PROGRAM WILL PRINT AaBbCcDdEe

PYTHON PROGRAM THAT ASKS USER FOR LARGE INTEGERS AND INSERTS COMMAS INTO IT ACCORDING TO THE STANDARD AMERICAN CONVENTION FOR COMMAS IN LARGE NUMBERS