{"id":10888,"date":"2021-09-30T15:00:08","date_gmt":"2021-09-30T09:30:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=10888"},"modified":"2021-11-22T18:34:30","modified_gmt":"2021-11-22T13:04:30","slug":"python-program-to-print-hollow-rectangle-star-pattern","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-hollow-rectangle-star-pattern\/","title":{"rendered":"Python Program to Print Hollow Rectangle Star Pattern"},"content":{"rendered":"
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java<\/a> and get a good grip on it for better results in exams or interviews.<\/p>\n Given the length, breadth of the rectangle the task is to print the hollow rectangle star pattern in C, C++, and Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n \u00a0<\/strong><\/p>\n Below are the ways to print the Hollow Rectangle star pattern in C, C++, and Python.<\/p>\n Approach:<\/strong><\/p>\n 1) Python Implementation<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2) C++Implementation<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3) C Implementation<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n 1) Python Implementation<\/strong><\/p>\n Give the length and breadth of the rectangle as user input using the int(input()) function and store them in two separate variables.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2) C++ Implementation<\/strong><\/p>\n Give the length and breadth of the rectangle as user input using the cin and store them in two separate variables.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3) C Implementation<\/strong><\/p>\n Give the length and breadth of the rectangle as user input using the scanf and store them in two separate variables.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Here we printed two blank spaces if the else condition is executed.<\/p>\n","protected":false},"excerpt":{"rendered":" Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews. Given the length, breadth of the rectangle the task is to print the hollow rectangle star pattern in C, C++, …<\/p>\ngiven length of rectangle =11\r\ngiven breadth of rectangle =19<\/pre>\n
* * * * * * * * * * * * * * * * * * * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * * * * * * * * * * * * * * * * * *<\/pre>\n
given length of rectangle =20\r\ngiven breadth of rectangle =5<\/pre>\n
* * * * * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * * * *<\/pre>\n
Program to Print Hollow Rectangle Star Pattern in C, C++, and Python<\/h2>\n
\n
Method #1: Using For loop (Static Input)<\/h3>\n
\n
# Give the length and breadth as static input and store them in two variables.\r\nlengthnum = 11\r\nbreadthnum = 19\r\n# Loop till the length of the rectangle using For loop.\r\nfor m in range(lengthnum):\r\n # Loop till the breadth of the rectangle using another nested For loop.\r\n for n in range(breadthnum):\r\n # We use the If Else statement to check If the length or breadth number is 0 or maximum \u2013 1.\r\n # If it is true then print * else print space.\r\n if(m == 0 or m == lengthnum - 1 or n == 0 or n == breadthnum - 1):\r\n print('*', end=' ')\r\n else:\r\n print(' ', end=' ')\r\n print()\r\n<\/pre>\n
* * * * * * * * * * * * * * * * * * * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * * * * * * * * * * * * * * * * * *<\/pre>\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n\r\n \/\/ Give the length and breadth as static input and store\r\n \/\/ them in two variables.\r\n int lengthnum = 11;\r\n int breadthnum = 23;\r\n \/\/ Loop till the length of the rectangle using For loop.\r\n for (int m = 0; m < lengthnum; m++) {\r\n \/\/ Loop till the breadth of the rectangle using\r\n \/\/ another nested For loop.\r\n for (int n = 0; n < breadthnum; n++) {\r\n \/\/ We use the If Else statement to check If the\r\n \/\/ length or breadth number is 0 or maximum \u2013 1.\r\n \/\/ If it is true then print * else print space.\r\n if (m == 0 || m == lengthnum - 1 || n == 0\r\n || n == breadthnum - 1)\r\n cout << \"* \";\r\n else\r\n cout << \" \";\r\n }\r\n cout << endl;\r\n }\r\n\r\n return 0;\r\n}<\/pre>\n
* * * * * * * * * * * * * * * * * * * * * * * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * * * * * * * * * * * * * * * * * * * * * *<\/pre>\n
#include <stdio.h>\r\n\r\nint main()\r\n{\r\n \/\/ Give the length and breadth as static input and store\r\n \/\/ them in two variables.\r\n int lengthnum = 20;\r\n int breadthnum = 5;\r\n \/\/ Loop till the length of the rectangle using For loop.\r\n for (int m = 0; m < lengthnum; m++) {\r\n \/\/ Loop till the breadth of the rectangle using\r\n \/\/ another nested For loop.\r\n for (int n = 0; n < breadthnum; n++) {\r\n \/\/ We use the If Else statement to check If the\r\n \/\/ length or breadth number is 0 or maximum \u2013 1.\r\n \/\/ If it is true then print * else print space.\r\n if (m == 0 || m == lengthnum - 1 || n == 0\r\n || n == breadthnum - 1)\r\n printf(\"* \");\r\n else\r\n printf(\" \");\r\n }\r\n printf(\"\\n\");\r\n }\r\n}<\/pre>\n
* * * * * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * * * *<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the length and breadth of the rectangle as user input using the int(input()) function\r\n# and store them in two separate variables.\r\nlengthnum = int(input('Enter some random length value of the rectangle = '))\r\n\r\nbreadthnum = int(input('Enter some random breadth value of the rectangle = '))\r\n# Loop till the length of the rectangle using For loop.\r\nfor m in range(lengthnum):\r\n # Loop till the breadth of the rectangle using another nested For loop.\r\n for n in range(breadthnum):\r\n # We use the If Else statement to check If the length or breadth number is 0 or maximum \u2013 1.\r\n # If it is true then print * else print space.\r\n if(m == 0 or m == lengthnum - 1 or n == 0 or n == breadthnum - 1):\r\n print('*', end=' ')\r\n else:\r\n print(' ', end=' ')\r\n print()\r\n<\/pre>\n
Enter some random length value of the rectangle = 20\r\nEnter some random breadth value of the rectangle = 5\r\n* * * * * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * * * *<\/pre>\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n\r\n \/\/ Give the length and breadth of the rectangle as user\r\n \/\/ input using the cin\r\n \/\/ and store them in two separate variables.\r\n int lengthnum, breadthnum;\r\n cout << \"Enter some random length and breadth of \"\r\n \"rectangle separated by spaces\"\r\n << endl;\r\n cin >> lengthnum >> breadthnum;\r\n \/\/ Loop till the length of the rectangle using For loop.\r\n for (int m = 0; m < lengthnum; m++) {\r\n \/\/ Loop till the breadth of the rectangle using\r\n \/\/ another nested For loop.\r\n for (int n = 0; n < breadthnum; n++) {\r\n \/\/ We use the If Else statement to check If the\r\n \/\/ length or breadth number is 0 or maximum \u2013 1.\r\n \/\/ If it is true then print * else print space.\r\n if (m == 0 || m == lengthnum - 1 || n == 0\r\n || n == breadthnum - 1)\r\n cout << \"* \";\r\n else\r\n cout << \" \";\r\n }\r\n cout << endl;\r\n }\r\n\r\n return 0;\r\n}<\/pre>\n
Enter some random length and breadth of rectangle separated by spaces\r\n11 19\r\n* * * * * * * * * * * * * * * * * * * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * * * * * * * * * * * * * * * * * *<\/pre>\n
#include <stdio.h>\r\n\r\nint main()\r\n{\r\n \/\/ Give the length and breadth of the rectangle as\r\n \/\/ user input using the scanf\r\n \/\/ and store them in two separate variables.\r\n int lengthnum, breadthnum;\r\n printf(\"Enter some random length and breadth of \"\r\n \"rectangle separated by spaces \\n\");\r\n scanf(\"%d%d\", &lengthnum, &breadthnum);\r\n \/\/ Loop till the length of the rectangle using For loop.\r\n for (int m = 0; m < lengthnum; m++) {\r\n \/\/ Loop till the breadth of the rectangle using\r\n \/\/ another nested For loop.\r\n for (int n = 0; n < breadthnum; n++) {\r\n \/\/ We use the If Else statement to check If the\r\n \/\/ length or breadth number is 0 or maximum \u2013 1.\r\n \/\/ If it is true then print * else print space.\r\n if (m == 0 || m == lengthnum - 1 || n == 0\r\n || n == breadthnum - 1)\r\n printf(\"* \");\r\n else\r\n printf(\" \");\r\n }\r\n printf(\"\\n\");\r\n }\r\n}<\/pre>\n
Enter some random length and breadth of rectangle separated by spaces\r\n11 23\r\n* * * * * * * * * * * * * * * * * * * * * * * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * \r\n* * * * * * * * * * * * * * * * * * * * * * *<\/pre>\n