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 :
Circle : Radius = 4
*****
** **
** **
* *
* *
* *
** **
** **
*****
Square : Side = 4
****
* *
* *
****
Rectangle : Length = 3, Breadth = 8
********
* *
********
Triangle : Side = 4
*
* *
* *
*******
Hexagon : Side = 4
****
******
********
**********
********
******
****
Follow the code below to print the different 2D shapes :Â
C++
// C++ implementation of menu driven// programme to print solid 2D shapes#include <bits/stdc++.h>using namespace std;Â
// Function to print a circlevoid circle(int radius){Â
    for (int i = 0; i <= 2 * radius; i++)    {        for (int j = 0; j <= 2 * radius; j++)        {            double distance = sqrt((double)(i - radius)                        * (i - radius) + (j - radius)                        * (j - radius));Â
            if (distance > radius - 0.5 &&                distance < radius + 0.5)                printf("*");            else                printf(" ");        }        printf("\n");    }}Â
// Function to print a square or a rectanglevoid rectangle(int l, int b){Â
    int i, j;    for (i = 1; i <= l; i++)    {        for (j = 1; j <= b; j++)            if (i == 1 || i == l || j == 1 || j == b)                printf("*");            else                printf(" ");        printf("\n");    }}Â
// Function to print trianglevoid triangle(int side){Â Â Â Â int i, j;Â Â Â Â for (i = 1; i <= side; i++)Â Â Â Â {Â Â Â Â Â Â Â Â for (j = i; j < side; j++)Â Â Â Â Â Â Â Â Â Â Â Â printf(" ");Â
        for (j = 1; j <= (2 * i - 1); j++)        {            if (i == side || j == 1 || j == (2 * i - 1))                printf("*");            else                printf(" ");        }        printf("\n");    }}Â
// Function to print hexagonvoid hexagon(int length){    int l, j, i, k;    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)                printf("*");            else                printf(" ");        printf("\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)                printf("*");            else                printf(" ");        printf("\n");    }}Â
// Function takes user choicevoid printPattern(int choice){Â
    int radius, length, breadth, side;    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:        printf("Wrong choice\n");    }}Â
// Driver Functionint main(){Â Â Â Â int choice = 3;Â Â Â Â printPattern(choice);Â Â Â Â return 0;} |
Java
// Java implementation of menu driven// programme to print solid 2D shapesclass GFG{    // Function to print a circle    static void circle(int radius)    {         for (int i = 0; i <= 2 * radius; i++)        {            for (int j = 0; j <= 2 * radius; j++)            {                double distance =                   Math.sqrt((double)(i - radius)                        * (i - radius) + (j - radius)                                        * (j - radius));                     if (distance > radius - 0.5 &&                        distance < radius + 0.5)                                     System.out.print("*");                else                    System.out.print(" ");            }                         System.out.println();        }    }         // Function to print a square or a rectangle    static void rectangle(int l, int b)    {             int i, j;                 for (i = 1; i <= l; i++)        {            for (j = 1; j <= b; j++)                if (i == 1 || i == l || j == 1 || j == b)                    System.out.print("*");                else                    System.out.print(" ");                     System.out.println();        }    }         // Function to print triangle    static void triangle(int side)    {        int i, j;                 for (i = 1; i <= side; i++)        {            for (j = i; j < side; j++)                   System.out.print(" ");                 for (j = 1; j <= (2 * i - 1); j++)            {                if (i == side || j == 1 ||                                j == (2 * i - 1))                    System.out.print("*");                else                    System.out.print(" ");            }            System.out.println();        }    }         // Function to print hexagon    static void hexagon(int length)    {        int l, j, i, k;        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)                    System.out.print("*");                else                    System.out.print(" ");            System.out.println();        }        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)                    System.out.print("*");                else                    System.out.print(" ");            System.out.println();        }    }         // Function takes user choice    static void printPattern(int choice)    {             int radius, length, breadth, side;                 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:            System.out.print("Wrong choice\n");        }    }Â
// Driver Program to test above functionpublic static void main(String arg[]){Â Â Â Â int choice = 3;Â Â Â Â printPattern(choice);}}Â
// This code is contributed by Anant Agarwal. |
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) |
C#
// C# implementation of menu driven // programme to print solid 2D shapes using System; class GFG {     // Function to print a circle     static void circle(int radius)     {            for (int i = 0; i <= 2 * radius; i++)         {             for (int j = 0; j <= 2 * radius; j++)             {                 double distance =                 Math.Sqrt((double)(i - radius)                         * (i - radius) + (j - radius)                                         * (j - radius));                        if (distance > radius - 0.5 &&                         distance < radius + 0.5)                                        Console.Write("*");                 else                    Console.Write(" ");             }                            Console.WriteLine();         }     }            // Function to print a square or a rectangle     static void rectangle(int l, int b)     {                int i, j;                    for (i = 1; i <= l; i++)         {             for (j = 1; j <= b; j++)                 if (i == 1 || i == l || j == 1 || j == b)                     Console.Write("*");                 else                    Console.Write(" ");                                Console.WriteLine();         }     }            // Function to print triangle     static void triangle(int side)     {         int i, j;                    for (i = 1; i <= side; i++)         {             for (j = i; j < side; j++)                 Console.Write(" ");                    for (j = 1; j <= (2 * i - 1); j++)             {                 if (i == side || j == 1 ||                                 j == (2 * i - 1))                     Console.Write("*");                 else                    Console.Write(" ");             }                   Console.WriteLine();         }     }            // Function to print hexagon     static void hexagon(int length)     {         int l, j, i, k;         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)                     Console.Write("*");                 else                    Console.Write(" ");                     Console.WriteLine();         }         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)                     Console.Write("*");                 else                    Console.Write(" ");                     Console.WriteLine();         }     }            // Function takes user choice     static void printPattern(int choice)     {                int radius, length, breadth, side;                    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:         Console.Write("Wrong choice");         break;         }     }    // Driver Program to test above function public static void Main() {     int choice = 3;     printPattern(choice); } }    // This code is contributed by vt_m. |
PHP
<?php// PHP implementation to // print solid 2D shapesÂ
// Function to print a circlefunction circle($radius){Â Â Â Â for ($i = 0; $i <= 2 * $radius; $i++)Â Â Â Â {Â Â Â Â Â Â Â Â for ($j = 0; $j <= 2 * $radius; $j++)Â Â Â Â Â Â Â Â {Â Â Â Â Â Â Â Â Â Â Â Â $distance = sqrt(($i - $radius) * Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ($i - $radius) + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ($j - $radius) * Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ($j - $radius));Â
            if ($distance > $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?> |
Javascript
// Function to print a circlefunction circle(radius) {for (let i = 0; i <= 2 * radius; i++) {let row = '';for (let j = 0; j <= 2 * radius; j++) {const distance = Math.sqrt((i - radius) ** 2 + (j - radius) ** 2);if (distance > radius - 0.5 && distance < radius + 0.5) {row += '*';} else {row += ' ';}}console.log(row);}}Â
// Function to print a square or a rectanglefunction rectangle(length, breadth) {for (let i = 1; i <= length; i++) {let row = '';for (let j = 1; j <= breadth; j++) {if (i === 1 || i === length || j === 1 || j === breadth) {row += '*';} else {row += ' ';}}console.log(row);}}Â
// Function to print trianglefunction triangle(side) {for (let i = 1; i <= side; i++) {let row = '';for (let j = i; j < side; j++) {row += ' ';}for (let j = 1; j <= 2 * i - 1; j++) {if (i === side || j === 1 || j === 2 * i - 1) {row += '*';} else {row += ' ';}}console.log(row);}}Â
// Function to print hexagonfunction hexagon(length) {for (let i = 1, k = length, l = 2 * length - 1; i < length; i++, k--, l++) {let row = '';for (let j = 0; j < 3 * length; j++) {if (j >= k && j <= l) {row += '';} else {row += ' ';}}console.log(row);}for (let i = 0, k = 1, l = 3 * length - 2; i < length; i++, k++, l--) {let row = '';for (let j = 0; j < 3 * length; j++) {if (j >= k && j <= l) {row += '';} else {row += ' ';}}console.log(row);}}Â
// Function takes user choicefunction printPattern(choice) {let radius, length, breadth, side;switch (choice) {// For Circlecase 1:radius = 4;circle(radius);break;// For rectangle/squarecase 2:length = 3;breadth = 8;rectangle(length, breadth);break;// For trianglecase 3:side = 6;triangle(side);break;// For hexagoncase 4:side = 4;hexagon(side);break;default:console.log('Wrong choice');}}Â
// Driver Program to test above functionconst choice = 3;printPattern(choice); |
Output :
*
* *
* *
* *
* *
***********
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
