# Program to print 2D shapes

• Last Updated : 26 Jul, 2018

Take the user choice and print 2D shapes based on the choice.

A shape with only two dimensions (such as width and height) and no thickness. Squares, Circles, Triangles etc are two dimensional objects. These shapes mostly contain mathematical figures such are a line, square, triangle, rectangle and hexagon.

The shapes are given below :

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

Square :  Side = 4

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

Rectangle : Length = 3, Breadth = 8

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

Triangle : Side = 4

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

Hexagon : Side = 4

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Follow the code below to print the different 2D shapes :

## Python3

 # Python implementation to # print solid 2D shapesimport math  # def to print a circledef circle(radius) :      for i in range(0, 2 *                    radius + 1) :              for j in range(0, 2 *                        radius + 1) :                      distance = math.sqrt((i - radius) *                                 (i - radius) +                                 (j - radius) *                                 (j - radius))              if (distance > radius - 0.5 and                distance < radius + 0.5) :                print ("*", end = "")                          else :                print (" ", end = "")                   print ()  # def to print a # square or a rectangledef rectangle(l, b) :      for i in range(1, l + 1) :              for j in range(1, b + 1) :            if (i == 1 or i == l or                j == 1 or j == b) :                print ("*", end = "")                          else :                print (" ", end = "")        print ()      # def to print triangledef triangle(side) :      for i in range(1, side + 1) :              for j in range(i, side) :            print (" ", end = "")          for j in range(1, 2 * i) :                      if (i == side or j == 1 or                j == (2 * i - 1)) :                print ("*", end = "")                      else :                print (" ", end = "")                  print ()  # def to print hexagondef hexagon(length) :      k = length    l = 2 * length - 1    for i in range(1, length) :              for j in range(0, 3 * length) :            if (j >= k and j <= l) :                print ("*", end = "")                          else :                print (" ", end = "")        print ()        k = k - 1        l = l + 1          k = 1    l = 3 * length - 2    for i in range(0, length) :          for j in range(0, 3 * length) :            if (j >= k and j <= l) :                print ("*", end = "")            else :                print (" ", end = "")        print ()        k = k + 1        l = l - 1  # def takes user choicedef printPattern(choice) :          # For Circle    if(choice == 1) :        circle(4)          # For rectangle/square    elif(choice == 2) :        rectangle(3, 8)          # For triangle     elif(choice == 3) :        triangle(6)          # For hexagon    elif(choice == 4) :        hexagon(4)  # Driver Codechoice = 3printPattern(choice)  # This code is contributed by# Manish Shaw(manishshaw1)

## PHP

 \$radius - 0.5 &&                \$distance < \$radius + 0.5)                echo "*";                          else                echo " ";        }        echo "\n";    }}  // Function to print a // square or a rectanglefunction rectangle(\$l, \$b){      for (\$i = 1; \$i <= \$l; \$i++)    {        for (\$j = 1; \$j <= \$b; \$j++)            if (\$i == 1 || \$i == \$l ||                 \$j == 1 || \$j == \$b)                echo "*";                          else                echo " ";        echo "\n";    }}  // Function to print trianglefunction triangle(\$side){    for (\$i = 1; \$i <= \$side; \$i++)    {        for (\$j = \$i; \$j < \$side; \$j++)            echo " ";          for (\$j = 1; \$j <= (2 * \$i - 1);                                   \$j++)        {            if (\$i == \$side || \$j == 1 ||                 \$j == (2 * \$i - 1))                echo "*";                      else                echo " ";        }        echo "\n";    }}  // Function to print hexagonfunction hexagon(\$length){      for (\$i = 1, \$k = \$length,          \$l = 2 * \$length - 1;          i < \$length; \$i++, \$k--, \$l++)    {        for (\$j = 0; \$j < 3 * \$length; \$j++)            if (\$j >= \$k && \$j <= \$l)                echo "*";                          else                echo " ";        echo "\n";    }    for (\$i = 0, \$k = 1, \$l = 3 * \$length - 2;               \$i < \$length; \$i++, \$k++, \$l--)    {        for (\$j = 0; \$j < 3 * \$length; \$j++)            if (\$j >= \$k && \$j <= \$l)                echo "*";            else                echo " ";        echo "\n";    }}  // Function takes user choicefunction printPattern(\$choice){    switch (\$choice)    {              // For Circle    case 1:        \$radius = 4;        circle(\$radius);        break;      // For rectangle/square    case 2:        \$length = 3;        \$breadth = 8;        rectangle(\$length, \$breadth);        break;      // For triangle    case 3:        \$side = 6;        triangle(\$side);        break;      // For hexagon    case 4:        \$side = 4;        hexagon(\$side);        break;          default:        echo "Wrong choice\n";    }}  // Driver Code\$choice = 3;printPattern(\$choice);  // This code is contributed by Mithun Kumar?>

Output :

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

My Personal Notes arrow_drop_up