Write a program to create a pyramid of the character ‘*’ and a reverse pyramid

by

Last updated on Nov 5, 2022
Python Practicals (Question 3)

Input

def pyramid(n):
    k = n - 1
    for i in range(n):
        for j in range(0, k):
            print(end="  ")
        k -= 1
        for j in range(0, (2 * i) + 1):
            print("* ", end='')
        print("\r")


num = 5
print("The original pyramid is:")
pyramid(num)


def reverse(m):
    k = 0
    for i in range(m - 1, -1, -1):
        for j in range(k, 0, -1):
            print(end="  ")
        k += 1
        for j in range(0, (2 * i) + 1):
            print("* ", end='')
        print("\r")


num2 = 5
print("\nThe reversed pyramid is:")
reverse(num2)

Output

The original pyramid is:
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 

The reversed pyramid is:
* * * * * * * * * 
  * * * * * * * 
    * * * * * 
      * * * 
        * 

How useful was this post?

5 star mean very useful & 1 star means not useful at all.

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Tags: