{"id":12027,"date":"2021-09-30T14:30:21","date_gmt":"2021-09-30T09:00:21","guid":{"rendered":"https:\/\/python-programs.com\/?p=12027"},"modified":"2021-11-22T18:34:33","modified_gmt":"2021-11-22T13:04:33","slug":"python-program-to-print-hollow-diamond-star-pattern","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-hollow-diamond-star-pattern\/","title":{"rendered":"Python Program to Print Hollow Diamond Star Pattern"},"content":{"rendered":"
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs<\/a> examples and excel in coding the complex logic.<\/p>\n Given the number of rows of the diamond pattern, the task is to print the Hollow diamond 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 Below are the ways to print Diamond 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 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 Related Programs<\/strong>:<\/p>\n Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic. Given the number of rows of the diamond pattern, the task is to print the Hollow diamond star pattern in C, C++, and Python Examples: Example1: …<\/p>\ngiven number of rows of diamond =6<\/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 number of rows of diamond =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^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^<\/pre>\n
Program to Print Diamond Star Pattern in C, C++, and Python<\/h2>\n
\n
Method #1: Using For loop (Star Character)<\/h3>\n
\n
# Give the number of rows as static input and store it in a variable.\r\ndiamondrows = 6\r\n\r\n# Loop from 1 to the number of rows using For Loop.\r\nfor m in range(1, diamondrows+1):\r\n # Loop from 1 to the number of rows -iterator value of the parent\r\n # For loop using another For loop(Nested For loop).\r\n for n in range(1, diamondrows - m + 1):\r\n # Print the space character in the inner For loop.\r\n print(end=' ')\r\n # Loop from 1 to the 2* iterator value of the parent For loop\r\n # using another For loop(Nested For loop).\r\n for l in range(1, (2 * m)):\r\n # If the value of the iterator is equal to 1 or 2* iterator value\r\n # of the parent loop -1 then print star character.\r\n if l == 1 or l == m * 2 - 1:\r\n print('*', end='')\r\n # Else print space character.\r\n else:\r\n print(' ', end='')\r\n # After the end of the inner for Loops print the Newline Character.\r\n print()\r\n\r\n# loop from number of rows -1 to 0 in decreasing order using For loop.\r\nfor m in range(diamondrows - 1, 0, -1):\r\n # Loop from 1 to number of rows- iterator value\r\n # of the parent loop using another For loop(Nested For loop)\r\n for n in range(1, diamondrows - m + 1):\r\n # Print the space character in the inner For loop.\r\n print(end=' ')\r\n # Loop from 1 to the 2* iterator value of the parent For loop\r\n # using another For loop(Nested For loop).\r\n for l in range(1, (2 * m)):\r\n # If the value of the iterator is equal to 1 or 2* iterator value\r\n # of the parent loop -1 then print star character.\r\n if l == 1 or l == m * 2 - 1:\r\n print('*', end='')\r\n # Else print space character.\r\n else:\r\n print(' ', end='')\r\n\r\n # After the end of the inner for Loops print the Newline Character.\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 number of rows of the diamond pattern\r\n \/\/ as static input and store it in a variable.\r\n int diamondrows = 6;\r\n char givencharacter = '*';\r\n \/\/ Loop from 1 to the number of rows using For Loop.\r\n for (int m = 1; m <= diamondrows; m++) {\r\n\r\n \/\/ Loop from 1 to the number of rows- iterator value\r\n \/\/ of the parent For loop using another For\r\n \/\/ loop(Nested For loop).\r\n for (int n = 1; n <= (diamondrows - m); n++) {\r\n\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n cout << \" \";\r\n }\r\n \/\/ Loop from 1 to the 2* iterator value of the\r\n \/\/ parent For loop\r\n \/\/ using another For loop(Nested For loop).\r\n for (int l = 1; l < (2 * m); l++) {\r\n \/\/ If the value of the iterator is equal to 1 or\r\n \/\/ 2* iterator value of the parent loop -1 then\r\n \/\/ print givencharacter .\r\n if (l == 1 || l == m * 2 - 1)\r\n cout << givencharacter;\r\n \/\/ Else print space character.\r\n else\r\n cout << \" \";\r\n }\r\n \/\/ After the end of the inner for Loops print the\r\n \/\/ Newline Character.\r\n cout << endl;\r\n }\r\n \/\/ loop from number of rows -1 to 0 in decreasing order using For loop.\r\n\r\n for (int m = diamondrows-1; m > 0; m--) {\r\n \/\/Loop from 1 to number of rows- iterator value\r\n \/\/ of the parent loop using another For loop(Nested For loop)\r\n for (int n = 1; n <= diamondrows-m; n++) {\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n cout << \" \";\r\n }\r\n \/\/ Loop from 1 to the 2* iterator value of the\r\n \/\/ parent For loop\r\n \/\/ using another For loop(Nested For loop).\r\n for (int l = 1; l < (2 * m); l++) {\r\n \/\/ If the value of the iterator is equal to 1 or\r\n \/\/ 2* iterator value of the parent loop -1 then\r\n \/\/ print givencharacter .\r\n if (l == 1 || l == m * 2 - 1)\r\n cout << givencharacter;\r\n \/\/ Else print space character.\r\n else\r\n cout << \" \";\r\n }\r\n\r\n \/\/ After the end of the inner for Loops print the\r\n \/\/ Newline Character.\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 char givencharacter = '*';\r\n \/\/ Give the number of rows of the diamond pattern\r\n \/\/ as static input and store it in a variable.\r\n int diamondrows = 6;\r\n \/\/ Loop from 1 to the number of rows using For Loop.\r\n for (int m = 1; m <= diamondrows; m++) {\r\n\r\n \/\/ Loop from 1 to the number of rows- iterator value\r\n \/\/ of the parent For loop using another For\r\n \/\/ loop(Nested For loop).\r\n for (int n = 1; n <= (diamondrows - m); n++) {\r\n\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n printf(\" \");\r\n }\r\n \/\/ Loop from 1 to the 2* iterator value of the\r\n \/\/ parent For loop\r\n \/\/ using another For loop(Nested For loop).\r\n for (int l = 1; l < (2 * m); l++) {\r\n \/\/ If the value of the iterator is equal to 1 or\r\n \/\/ 2* iterator value of the parent loop -1 then\r\n \/\/ print givencharacter .\r\n if (l == 1 || l == m * 2 - 1)\r\n printf(\"%c\", givencharacter);\r\n \/\/ Else print space character.\r\n else\r\n printf(\" \");\r\n }\r\n \/\/ After the end of the inner for Loops print the\r\n \/\/ Newline Character.\r\n printf(\"\\n\");\r\n }\r\n \/\/ loop from number of rows -1 to 0 in decreasing order\r\n \/\/ using For loop.\r\n\r\n for (int m = diamondrows - 1; m > 0; m--) {\r\n \/\/ Loop from 1 to number of rows- iterator value\r\n \/\/ of the parent loop using another For loop(Nested\r\n \/\/ For loop)\r\n for (int n = 1; n <= diamondrows - m; n++) {\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n printf(\" \");\r\n }\r\n \/\/ Loop from 1 to the 2* iterator value of the\r\n \/\/ parent For loop\r\n \/\/ using another For loop(Nested For loop).\r\n for (int l = 1; l < (2 * m); l++) {\r\n \/\/ If the value of the iterator is equal to 1 or\r\n \/\/ 2* iterator value of the parent loop -1 then\r\n \/\/ print givencharacter .\r\n if (l == 1 || l == m * 2 - 1)\r\n printf(\"%c\", givencharacter);\r\n \/\/ Else print space character.\r\n else\r\n printf(\" \");\r\n }\r\n\r\n \/\/ After the end of the inner for Loops print the\r\n \/\/ Newline Character.\r\n printf(\"\\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
Method #2: Using For loop (User Character)<\/h3>\n
\n
\n
# Give the number of rows as user input using int(input()) and store it in a variable.\r\ndiamondrows = int(input(\r\n 'Enter some random number of rows = '))\r\n# Give the Character as user input using input() and store it in another variable.\r\ngivencharacter = input('Enter some random character = ')\r\n# Loop from 1 to the number of rows using For Loop.\r\nfor m in range(1, diamondrows+1):\r\n # Loop from 1 to the number of rows -iterator value of the parent\r\n # For loop using another For loop(Nested For loop).\r\n for n in range(1, diamondrows - m + 1):\r\n # Print the space character in the inner For loop.\r\n print(end=' ')\r\n # Loop from 1 to the 2* iterator value of the parent For loop\r\n # using another For loop(Nested For loop).\r\n for l in range(1, (2 * m)):\r\n # If the value of the iterator is equal to 1 or 2* iterator value\r\n # of the parent loop -1 then print givencharacter .\r\n if l == 1 or l == m * 2 - 1:\r\n print(givencharacter, end='')\r\n # Else print space character.\r\n else:\r\n print(' ', end='')\r\n # After the end of the inner for Loops print the Newline Character.\r\n print()\r\n\r\n# loop from number of rows -1 to 0 in decreasing order using For loop.\r\nfor m in range(diamondrows - 1, 0, -1):\r\n # Loop from 1 to number of rows- iterator value\r\n # of the parent loop using another For loop(Nested For loop)\r\n for n in range(1, diamondrows - m + 1):\r\n # Print the space character in the inner For loop.\r\n print(end=' ')\r\n # Loop from 1 to the 2* iterator value of the parent For loop\r\n # using another For loop(Nested For loop).\r\n for l in range(1, (2 * m)):\r\n # If the value of the iterator is equal to 1 or 2* iterator value\r\n # of the parent loop -1 then print givencharacter .\r\n if l == 1 or l == m * 2 - 1:\r\n print(givencharacter, end='')\r\n # Else print space character.\r\n else:\r\n print(' ', end='')\r\n\r\n # After the end of the inner for Loops print the Newline Character.\r\n print()\r\n<\/pre>\n
Enter some random number of rows = 10\r\nEnter some random character = ^ \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
\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n\r\n \/\/ Give the number of rows\r\n \/\/ as user input using cin and store it in a\r\n \/\/ variable.\r\n int diamondrows;\r\n char givencharacter;\r\n cout << \"Enter some random number of rows = \" << endl;\r\n cin >> diamondrows;\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 = \" << endl;\r\n cin >> givencharacter;\r\n cout << endl;\r\n \/\/ Loop from 1 to the number of rows using For Loop.\r\n for (int m = 1; m <= diamondrows; m++) {\r\n\r\n \/\/ Loop from 1 to the number of rows- iterator value\r\n \/\/ of the parent For loop using another For\r\n \/\/ loop(Nested For loop).\r\n for (int n = 1; n <= (diamondrows - m); n++) {\r\n\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n cout << \" \";\r\n }\r\n \/\/ Loop from 1 to the 2* iterator value of the\r\n \/\/ parent For loop\r\n \/\/ using another For loop(Nested For loop).\r\n for (int l = 1; l < (2 * m); l++) {\r\n \/\/ If the value of the iterator is equal to 1 or\r\n \/\/ 2* iterator value of the parent loop -1 then\r\n \/\/ print givencharacter .\r\n if (l == 1 || l == m * 2 - 1)\r\n cout << givencharacter;\r\n \/\/ Else print space character.\r\n else\r\n cout << \" \";\r\n }\r\n \/\/ After the end of the inner for Loops print the\r\n \/\/ Newline Character.\r\n cout << endl;\r\n }\r\n \/\/ loop from number of rows -1 to 0 in decreasing order using For loop.\r\n\r\n for (int m = diamondrows-1; m > 0; m--) {\r\n \/\/Loop from 1 to number of rows- iterator value\r\n \/\/ of the parent loop using another For loop(Nested For loop)\r\n for (int n = 1; n <= diamondrows-m; n++) {\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n cout << \" \";\r\n }\r\n \/\/ Loop from 1 to the 2* iterator value of the\r\n \/\/ parent For loop\r\n \/\/ using another For loop(Nested For loop).\r\n for (int l = 1; l < (2 * m); l++) {\r\n \/\/ If the value of the iterator is equal to 1 or\r\n \/\/ 2* iterator value of the parent loop -1 then\r\n \/\/ print givencharacter .\r\n if (l == 1 || l == m * 2 - 1)\r\n cout << givencharacter;\r\n \/\/ Else print space character.\r\n else\r\n cout << \" \";\r\n }\r\n\r\n \/\/ After the end of the inner for Loops print the\r\n \/\/ Newline Character.\r\n cout << endl;\r\n }\r\n return 0;\r\n}<\/pre>\n
Enter some random number of rows = \r\n10\r\nEnter some random character = \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
\n
#include <stdio.h>\r\n\r\nint main()\r\n{\r\n \/\/ Give the number of rows\r\n \/\/ as user input using scanf and store it in a\r\n \/\/ variable.\r\n int diamondrows;\r\n char givencharacter;\r\n \/\/ Give the Character as user input using scanf and\r\n \/\/ store it in another variable.\r\n scanf(\"%d\", &diamondrows);\r\n scanf(\"%c\", &givencharacter);\r\n \/\/ Loop from 1 to the number of rows using For Loop.\r\n for (int m = 1; m <= diamondrows; m++) {\r\n\r\n \/\/ Loop from 1 to the number of rows- iterator value\r\n \/\/ of the parent For loop using another For\r\n \/\/ loop(Nested For loop).\r\n for (int n = 1; n <= (diamondrows - m); n++) {\r\n\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n printf(\" \");\r\n }\r\n \/\/ Loop from 1 to the 2* iterator value of the\r\n \/\/ parent For loop\r\n \/\/ using another For loop(Nested For loop).\r\n for (int l = 1; l < (2 * m); l++) {\r\n \/\/ If the value of the iterator is equal to 1 or\r\n \/\/ 2* iterator value of the parent loop -1 then\r\n \/\/ print givencharacter .\r\n if (l == 1 || l == m * 2 - 1)\r\n printf(\"%c\", givencharacter);\r\n \/\/ Else print space character.\r\n else\r\n printf(\" \");\r\n }\r\n \/\/ After the end of the inner for Loops print the\r\n \/\/ Newline Character.\r\n printf(\"\\n\");\r\n }\r\n \/\/ loop from number of rows -1 to 0 in decreasing order\r\n \/\/ using For loop.\r\n\r\n for (int m = diamondrows - 1; m > 0; m--) {\r\n \/\/ Loop from 1 to number of rows- iterator value\r\n \/\/ of the parent loop using another For loop(Nested\r\n \/\/ For loop)\r\n for (int n = 1; n <= diamondrows - m; n++) {\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n printf(\" \");\r\n }\r\n \/\/ Loop from 1 to the 2* iterator value of the\r\n \/\/ parent For loop\r\n \/\/ using another For loop(Nested For loop).\r\n for (int l = 1; l < (2 * m); l++) {\r\n \/\/ If the value of the iterator is equal to 1 or\r\n \/\/ 2* iterator value of the parent loop -1 then\r\n \/\/ print givencharacter .\r\n if (l == 1 || l == m * 2 - 1)\r\n printf(\"%c\", givencharacter);\r\n \/\/ Else print space character.\r\n else\r\n printf(\" \");\r\n }\r\n\r\n \/\/ After the end of the inner for Loops print the\r\n \/\/ Newline Character.\r\n printf(\"\\n\");\r\n }\r\n return 0;\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^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^ ^\r\n ^<\/pre>\n
\n