Program to print 2D shapes
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 circle void 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 rectangle 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) printf("*"); else printf(" "); printf("\n"); } } // Function to print triangle void 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 hexagon 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) 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 choice 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: printf("Wrong choice\n"); } } // Driver Function int main() { int choice = 3; printPattern(choice); return 0; } |
Java
// Java implementation of menu driven // programme to print solid 2D shapes 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) 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 function public static void main(String arg[]) { int choice = 3; printPattern(choice); } } // This code is contributed by Anant Agarwal. |
Python3
# Python implementation to # print solid 2D shapes import math # def to print a circle def 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 rectangle def 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 triangle def 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 hexagon def 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 choice def 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 Code choice = 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 circle function 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 rectangle function 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 triangle function 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 hexagon function 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 choice function 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 :
*
* *
* *
* *
* *
***********
Recommended Posts:
- Program to print Hut
- Program to print the given H Pattern
- Program to print the Sum of series -1 + 2 + 11 + 26 + 47 +.....
- Program to print pattern
- Program to print the pattern 'G'
- Program to print the pattern ‘D’
- Program to print DNA sequence
- Program to print the given Z Pattern
- Program to print the pattern "GFG"
- Program to print the arrow pattern
- Program to print the series 2, 1, 4, 3, 6, 5, .... up to N terms
- Program to print Kite Pattern
- Program to print the Zigzag pattern
- Program to print Crown Pattern
- Program to print Sum of even and odd elements in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



