Open In App

Python program to print the Inverted heart pattern

Last Updated : 05 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Let us see how to print an inverted heart pattern in Python.

Example:

Input: 11
Output:

          *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************
*********************
 *********  ********
  *******    ******
   *****      **** 
   
Input: 15
Output:
              *
             ***
            *****
           *******
          *********
         ***********
        *************
       ***************
      *****************
     *******************
    *********************
   ***********************
  *************************
 ***************************
*****************************
 *************  ************
  ***********    **********
   *********      ********
    *******        ******

Approach:

  1. Determine the size of the heart.
  2. Print an inverted triangle with size number of rows.
  3. Print the rest of the heart in 4 segments inside another loop.
  4. Print the white space right-triangle at the beginning.
  5. Print the first trapezium with stars.
  6. Print the white space triangle.
  7. Print the second trapezium with stars.
Python3
# determining the size of the heart
size = 15

# printing the inverted triangle
for a in range(0, size):
    for b in range(a, size):
        print(" ", end = "")
    for b in range(1, (a * 2)): 
        print("*", end = "") 
    print("")

# printing rest of the heart
for a in range(size, int(size / 2) - 1 , -2):

    # printing the white space right-triangle
    for b in range(1, size - a, 2):  
        print(" ", end = "")

    # printing the first trapezium
    for b in range(1, a + 1): 
        print("*", end = "")

    # printing the white space triangle
    for b in range(1, (size - a) + 1): 
        print(" ", end = "")

    # printing the second trapezium
    for b in range(1, a): 
        print("*", end = "")

    # new line
    print("")

Output:

              *
             ***
            *****
           *******
          *********
         ***********
        *************
       ***************
      *****************
     *******************
    *********************
   ***********************
  *************************
 ***************************
*****************************
 *************  ************
  ***********    **********
   *********      ********
    *******        ******


 Time complexity: O(S2) for given input size S

Auxiliary Space: O(1)


Next Article

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy