{"id":11111,"date":"2021-09-30T15:00:27","date_gmt":"2021-09-30T09:30:27","guid":{"rendered":"https:\/\/python-programs.com\/?p=11111"},"modified":"2021-11-22T18:34:28","modified_gmt":"2021-11-22T13:04:28","slug":"python-program-to-print-hollow-square-star-with-diagonals","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-hollow-square-star-with-diagonals\/","title":{"rendered":"Python Program to Print Hollow Square Star With Diagonals"},"content":{"rendered":"
Beginners and experienced programmers can rely on these Best Java Programs Examples<\/a> and code various basic and complex logics in the Java programming language with ease.<\/p>\n Given the sides of the square, the task is to print the hollow Square star pattern with diagonals 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 Below are the ways to print the hollow square star with Diagonals pattern in C, C++, and Python.<\/p>\n Approach:<\/strong><\/p>\n 1)\u00a0 Python Implementation<\/strong><\/p>\n Below is the implementation: <\/p>\n Output:<\/strong><\/p>\n 2) C++Implementation<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n 3) C Implementation<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n 1) Python Implementation<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n 2) C++Implementation<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n 3) C Implementation<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease. Given the sides of the square, the task is to print the hollow Square star pattern with diagonals in C, C++, and Python. Examples: Example1: Input: given number of …<\/p>\ngiven number of sides of square =10<\/pre>\n
* * * * * * * * * * \r\n* * * * \r\n* * * * \r\n* * * * \r\n* * * * \r\n* * * * \r\n* * * * \r\n* * * * \r\n* * * * \r\n* * * * * * * * * *<\/pre>\n
given number of sides of square =10\r\ngiven character to print =$<\/pre>\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 Square Star Pattern with Diagonals in C, C++, and Python<\/h2>\n
\n
Method #1: Using For loop (Star Character)<\/h3>\n
\n
\n<\/strong><\/p>\n# Give the side of the square as static input and store it in a variable.\r\nsquareside = 10\r\n# Loop till the side length of the square using For loop.\r\nfor m in range(squareside):\r\n # Loop till the side length of the square using another nested For loop.\r\n for n in range(squareside):\r\n # We use the If Else statement to check If the side length is 0 or maximum \u2013 1. (For the Outer Boundary of the square)\r\n # We can say it is diagonal if a row equals a column, or a row equals N-i+1 (where i is the current row number).\r\n # We merge these two conditions using if and or operator.\r\n # We have or operator in Python,|| operator in C, C++, and Java\r\n # If it is true then print * else print space.\r\n if(m == 0 or m == squareside - 1 or n == 0 or n == squareside - 1 or m == n or n == (squareside - 1 - m)):\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* * * * * * * * * *<\/pre>\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n\r\n \/\/ Give the side of the square as static input and store\r\n \/\/ it in a variable.\r\n int squareside = 10;\r\n \/\/ Loop till the side length of the square using For\r\n \/\/ loop.\r\n for (int m = 0; m < squareside; m++) {\r\n \/\/ Loop till the side length of the square using\r\n \/\/ another nested For loop.\r\n for (int n = 0; n < squareside; n++) {\r\n \/\/ We use the If Else statement to check If the\r\n \/\/ side length is 0 or maximum \u2013 1.\r\n if (m == 0 || m == squareside - 1 || n == 0\r\n || n == squareside - 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* * * * * * * * * *<\/pre>\n
#include <stdio.h>\r\n\r\nint main()\r\n{\r\n \/\/ Give the side of the square as static input and store\r\n \/\/ it in a variable.\r\n int squareside = 10;\r\n \/\/ Loop till the side length of the square using For\r\n \/\/ loop.\r\n for (int m = 0; m < squareside; m++) {\r\n \/\/ Loop till the side length of the square using\r\n \/\/ another nested For loop.\r\n for (int n = 0; n < squareside; n++) {\r\n \/*We use the If Else statement to check If the\r\n side length is 0 or maximum \u2013 1. (For the\r\n Outer Boundary of the square) We can say it\r\n is diagonal if a row equals a column, or a\r\n row equals N-i+1 (where i is the current row\r\n number). We merge these two conditions using\r\n if and or operator. We have or operator in\r\n Python,|| operator in C,\r\n C++, and Java If it is true then print *\r\n else print space.*\/\r\n if (m == 0 || m == squareside - 1 || n == 0\r\n || n == squareside - 1 || m == n\r\n || n == (squareside - 1 - m))\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* * * * * * * * * *<\/pre>\n
Method #2: Using For Loop (User Character)<\/h3>\n
\n
\n
# Give the side of the square as user input and store it in a variable.\r\nsquareside = int(input('Enter some random number of sides of square = '))\r\n# Scan the character to print as user input and store it in a variable.\r\ncharacte = input('Enter some random character to print = ')\r\n# Loop till the side length of the square using For loop.\r\nfor m in range(squareside):\r\n # Loop till the side length of the square using another nested For loop.\r\n for n in range(squareside):\r\n # We use the If Else statement to check If the side length is 0 or maximum \u2013 1. (For the Outer Boundary of the square)\r\n # We can say it is diagonal if a row equals a column, or a row equals N-i+1 (where i is the current row number).\r\n # We merge these two conditions using if and or operator.\r\n # We have or operator in Python,|| operator in C, C++, and Java\r\n # If it is true then print * else print space.\r\n if(m == 0 or m == squareside - 1 or n == 0 or n == squareside - 1 or m == n or n == (squareside - 1 - m)):\r\n print(characte, end=' ')\r\n else:\r\n print(' ', end=' ')\r\n print()\r\n<\/pre>\n
Enter some random number of sides of square = 10\r\nEnter some random character to print = $\r\n$ $ $ $ $ $ $ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ $ $ $ $ $ $<\/pre>\n
\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main(void)\r\n{\r\n int sidesnum;\r\n char characte;\r\n \/\/ Give the number of sides of the square as user input\r\n \/\/ using cin and\r\n \/\/ store it in a variable.\r\n cout << \"Enter some random number of sides of the \"\r\n \"square = \"\r\n << endl;\r\n cin >> sidesnum;\r\n \/\/ Create a character variable.\r\n \/\/ Give the character as user input using cin and store\r\n \/\/ it in another variable.\r\n cout << \"Enter some random character to print = \"\r\n << endl;\r\n cin >> characte;\r\n cout << endl;\r\n\r\n \/\/ Using Nested For loops print the square pattern.\r\n for (int m = 0; m < sidesnum; m++) {\r\n for (int n = 0; n < sidesnum; n++) {\r\n \/*We use the If Else statement to check If the\r\n side length is 0 or maximum \u2013 1. (For the\r\n Outer Boundary of the square) We can say it\r\n is diagonal if a row equals a column, or a\r\n row equals N-i+1 (where i is the current row\r\n number). We merge these two conditions using\r\n if and or operator. We have or operator in\r\n Python,|| operator in C,\r\n C++, and Java If it is true then print *\r\n else print space.*\/\r\n if (m == 0 || m == sidesnum - 1 || n == 0\r\n || n == sidesnum - 1 || m == n\r\n || n == (sidesnum - 1 - m))\r\n cout << characte << \" \";\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 number of sides of the square = \r\n10\r\nEnter some random character to print = \r\n$\r\n$ $ $ $ $ $ $ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ $ $ $ $ $ $<\/pre>\n
\n
#include <stdio.h>\r\n\r\nint main()\r\n{\r\n int sidesnum;\r\n char characte;\r\n \/\/ Give the number of sides of the square as user input\r\n \/\/ using scanf and\r\n \/\/ store it in a variable.\r\n \/\/ Create a character variable.\r\n \/\/ Give the character as user input using scanf and\r\n \/\/ store it in another variable.\r\n \/\/ Using Nested For loops print the square pattern.\r\n scanf(\"%d%c\", &sidesnum, &characte);\r\n printf(\"\\n\");\r\n\r\n for (int m = 0; m < sidesnum; m++) {\r\n for (int n = 0; n < sidesnum; n++) {\r\n \/*We use the If Else statement to check If the\r\n side length is 0 or maximum \u2013 1. (For the\r\n Outer Boundary of the square) We can say it\r\n is diagonal if a row equals a column, or a\r\n row equals N-i+1 (where i is the current row\r\n number). We merge these two conditions using\r\n if and or operator. We have or operator in\r\n Python,|| operator in C,\r\n C++, and Java If it is true then print *\r\n else print space.*\/\r\n if (m == 0 || m == sidesnum - 1 || n == 0\r\n || n == sidesnum - 1 || m == n\r\n || n == (sidesnum - 1 - m))\r\n printf(\"%c \", characte);\r\n else\r\n printf(\" \");\r\n }\r\n printf(\"\\n\");\r\n }\r\n}\r\n<\/pre>\n
10$\r\n$ $ $ $ $ $ $ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ \r\n$ $ $ $ $ $ $ $ $ $<\/pre>\n
\n