Python: triangle of stars partten proramming.

 Question:

write a program in python to print the triangle of stars.

Answer:

=====Code  =========

num=int(input("Enter the number ::"))
i=1
for i in range(num):
    for j in range(i+1):
        print("* " , end = ' ')
    print(" ")

 

====== output is ======

HP-Notebook:~$ python3 pyramid.py

Enter the number :: 5
*   
*  *   
*  *  *   
*  *  *  *   
*  *  *  *  *   

Description:

The num is a variable which stores the value input if user.  

num=int(input("Enter the number ::"))

we have applied two loops. 

- First loop: Normal loop statement

- Second Loop: Nested Loop statement(loop inside the loop).

First loop:

First Loop will tell that how many rows will be there in the triangle.  the iteration of this loop will change the line for printing the stars  as shown in the output.

Second Loop:

Second loop will tell that how many starts will be print in single line ( one iteration of first loop ).

At the end normal print function used for change the line. it is under the first loop.

print(" ")

 

No comments:

Post a Comment