{"id":11622,"date":"2021-09-30T14:30:47","date_gmt":"2021-09-30T09:00:47","guid":{"rendered":"https:\/\/python-programs.com\/?p=11622"},"modified":"2021-11-22T18:34:31","modified_gmt":"2021-11-22T13:04:31","slug":"python-program-to-print-hollow-half-diamond-star-pattern","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-hollow-half-diamond-star-pattern\/","title":{"rendered":"Python Program to Print Hollow Half Diamond Star Pattern"},"content":{"rendered":"
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output<\/a><\/p>\n Given the number of rows of the diamond pattern, the task is to print the Hollow Half diamond 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 Hollow Half 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 Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output Given the number of rows of the diamond pattern, the task is to print the Hollow Half diamond Pattern in C, C++, and Python. Examples: Example1: Input: Given number of rows …<\/p>\nGiven number of rows =7<\/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*<\/pre>\n
Given number of rows =9\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-<\/pre>\n
Program to Print Hollow Half 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 of the number of diamond pattern as static input and store it in a variable.\r\nrowsnumber = 7\r\n# Loop from 0 to the number of rows using For loop.\r\nfor m in range(0, rowsnumber):\r\n # Loop till the first iterator value using another For loop(Nested For loop).\r\n for n in range(0, m+1):\r\n # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.\r\n # Check if the second loop iterator value is equal to 0 using the If Statement.\r\n # Combine these two conditions using or operator.\r\n if(m == n or n == 0):\r\n # If the statement is true then print star character with space.\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 loop print the Newline Character.\r\n print()\r\n# After the end of two For loops Loop from 1 to the number of rows using For loop.\r\nfor m in range(1, rowsnumber):\r\n # Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).\r\n for n in range(m, rowsnumber):\r\n # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.\r\n # Check if the second loop iterator value is equal to the number of rows using the If Statement.\r\n # Combine these two conditions using or operator.\r\n if(m == n or n == rowsnumber-1):\r\n # If the statement is true then print star character with space.\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 loop 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* * \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 number of diamond pattern \\\r\n \/\/ as static input and store it in a variable.\r\n int rowsnumber = 7;\r\n \/\/ Loop from 0 to the number of rows using For loop.\r\n for (int m = 0; m < rowsnumber; m++) {\r\n \/\/ Loop till the first iterator value using another\r\n \/\/ For loop(Nested For loop)\r\n for (int n = 0; n < m + 1; n++) {\r\n \/*\r\n Check if the first loop iterator value is equal\r\n to the second loop iterator value using the If\r\n Statement. Check if the second loop iterator\r\n value is equal to 0 using the If Statement.\r\n Combine these two conditions using or\r\n operator. If the statement is true then print\r\n star character with space. Else print space\r\n character.*\/\r\n if (m == n || n == 0)\r\n cout << \"* \";\r\n else\r\n cout << \" \";\r\n }\r\n \/\/ After the end of the inner for loop print the\r\n \/\/ Newline Character.\r\n cout << endl;\r\n }\r\n \/\/ After the end of two For loops Loop from 1 to the\r\n \/\/ number of rows using For loop.\r\n for (int m = 1; m < rowsnumber; m++)\r\n \/\/ Loop from the first iterator value to the given\r\n \/\/ number of rows using another For loop(Nested For\r\n \/\/ loop)\r\n {\r\n for (int n = m; n < rowsnumber; n++) {\r\n\r\n \/*\r\n Check if the first loop iterator value is equal\r\n to the second loop iterator value using the If\r\n Statement. Check if the second loop iterator\r\n value is equal to the number of rows using the\r\n If Statement. Combine these two conditions\r\n using or operator. If the statement is true\r\n then print star character with space. Else\r\n print space character.*\/\r\n if (m == n || n == rowsnumber - 1)\r\n cout << \"* \";\r\n else\r\n cout << \" \";\r\n }\r\n \/\/ After the end of the inner for loop print the\r\n \/\/ Newline Character.\r\n cout << endl;\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* * \r\n* * \r\n*<\/pre>\n
#include <stdio.h>\r\n\r\nint main()\r\n{\r\n\r\n \/\/Give the number of rows of the number of diamond pattern \\\r\n \/\/ as static input and store it in a variable.\r\n int rowsnumber = 7;\r\n \/\/ Loop from 0 to the number of rows using For loop.\r\n for (int m = 0; m < rowsnumber; m++) {\r\n \/\/ Loop till the first iterator value using another\r\n \/\/ For loop(Nested For loop)\r\n for (int n = 0; n < m + 1; n++) {\r\n\r\n \/*\r\n Check if the first loop iterator value is equal\r\n to the second loop iterator value using the If\r\n Statement. Check if the second loop iterator\r\n value is equal to 0 using the If Statement.\r\n Combine these two conditions using or\r\n operator. If the statement is true then print\r\n star character with space. Else print space\r\n character.*\/\r\n if (m == n || n == 0)\r\n printf(\"* \");\r\n else\r\n printf(\" \");\r\n }\r\n \/\/ After the end of the inner for loop print the\r\n \/\/ Newline Character.\r\n printf(\"\\n\");\r\n }\r\n \/\/ After the end of two For loops Loop from 1 to the\r\n \/\/ number of rows using For loop.\r\n for (int m = 1; m < rowsnumber; m++)\r\n \/\/ Loop from the first iterator value to the given\r\n \/\/ number of rows using another For loop(Nested For\r\n \/\/ loop)\r\n {\r\n for (int n = m; n < rowsnumber; n++) {\r\n\r\n \/*\r\n Check if the first loop iterator value is equal\r\n to the second loop iterator value using the If\r\n Statement. Check if the second loop iterator\r\n value is equal to the number of rows using the\r\n If Statement. Combine these two conditions\r\n using or operator. If the statement is true\r\n then print star character with space. Else\r\n print space character.*\/\r\n if (m == n || n == rowsnumber - 1)\r\n printf(\"* \");\r\n else\r\n printf(\" \");\r\n }\r\n \/\/ After the end of the inner for loop 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* * \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\nrowsnumber = 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 0 to the number of rows using For loop.\r\nfor m in range(0, rowsnumber):\r\n # Loop till the first iterator value using another For loop(Nested For loop).\r\n for n in range(0, m+1):\r\n # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.\r\n # Check if the second loop iterator value is equal to 0 using the If Statement.\r\n # Combine these two conditions using or operator.\r\n if(m == n or n == 0):\r\n # If the statement is true then print the given character with space.\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 loop print the Newline Character.\r\n print()\r\n# After the end of two For loops Loop from 1 to the number of rows using For loop.\r\nfor m in range(1, rowsnumber):\r\n # Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).\r\n for n in range(m, rowsnumber):\r\n # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.\r\n # Check if the second loop iterator value is equal to the number of rows using the If Statement.\r\n # Combine these two conditions using or operator.\r\n if(m == n or n == rowsnumber-1):\r\n # If the statement is true then print the given character with space.\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 loop print the Newline Character.\r\n print()\r\n<\/pre>\n
Enter some random number of rows = 9\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-<\/pre>\n
\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\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 rowsnumber;\r\n char givencharacter;\r\n cout << \"Enter some random number of rows = \" << endl;\r\n cin >> rowsnumber;\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 0 to the number of rows using For loop.\r\n for (int m = 0; m < rowsnumber; m++) {\r\n \/\/ Loop till the first iterator value using another\r\n \/\/ For loop(Nested For loop)\r\n for (int n = 0; n < m + 1; n++) {\r\n \/*\r\n Check if the first loop iterator value is equal\r\n to the second loop iterator value using the If\r\n Statement. Check if the second loop iterator\r\n value is equal to 0 using the If Statement.\r\n Combine these two conditions using or\r\n operator. If the statement is true then print\r\n given character with space. Else print space\r\n character.*\/\r\n if (m == n || n == 0)\r\n cout << givencharacter << \" \";\r\n else\r\n cout << \" \";\r\n }\r\n \/\/ After the end of the inner for loop print the\r\n \/\/ Newline Character.\r\n cout << endl;\r\n }\r\n \/\/ After the end of two For loops Loop from 1 to the\r\n \/\/ number of rows using For loop.\r\n for (int m = 1; m < rowsnumber; m++)\r\n \/\/ Loop from the first iterator value to the given\r\n \/\/ number of rows using another For loop(Nested For\r\n \/\/ loop)\r\n {\r\n for (int n = m; n < rowsnumber; n++) {\r\n\r\n \/*\r\n Check if the first loop iterator value is equal\r\n to the second loop iterator value using the If\r\n Statement. Check if the second loop iterator\r\n value is equal to the number of rows using the\r\n If Statement. Combine these two conditions\r\n using or operator. If the statement is true\r\n then print given character with space. Else\r\n print space character.*\/\r\n if (m == n || n == rowsnumber - 1)\r\n cout << givencharacter << \" \";\r\n else\r\n cout << \" \";\r\n }\r\n \/\/ After the end of the inner for loop 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 = 9\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-<\/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 rowsnumber;\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\", &rowsnumber);\r\n scanf(\"%c\", &givencharacter);\r\n \r\n \/\/ Loop from 0 to the number of rows using For loop.\r\n for (int m = 0; m < rowsnumber; m++) {\r\n \/\/ Loop till the first iterator value using another\r\n \/\/ For loop(Nested For loop)\r\n for (int n = 0; n < m + 1; n++) {\r\n\r\n \/*\r\n Check if the first loop iterator value is equal\r\n to the second loop iterator value using the If\r\n Statement. Check if the second loop iterator\r\n value is equal to 0 using the If Statement.\r\n Combine these two conditions using or\r\n operator. If the statement is true then print\r\n given character with space. Else print space\r\n character.*\/\r\n if (m == n || n == 0)\r\n printf(\"%c \", givencharacter);\r\n else\r\n printf(\" \");\r\n }\r\n \/\/ After the end of the inner for loop print the\r\n \/\/ Newline Character.\r\n printf(\"\\n\");\r\n }\r\n \/\/ After the end of two For loops Loop from 1 to the\r\n \/\/ number of rows using For loop.\r\n for (int m = 1; m < rowsnumber; m++)\r\n \/\/ Loop from the first iterator value to the given\r\n \/\/ number of rows using another For loop(Nested For\r\n \/\/ loop)\r\n {\r\n for (int n = m; n < rowsnumber; n++) {\r\n\r\n \/*\r\n Check if the first loop iterator value is equal\r\n to the second loop iterator value using the If\r\n Statement. Check if the second loop iterator\r\n value is equal to the number of rows using the\r\n If Statement. Combine these two conditions\r\n using or operator. If the statement is true\r\n then print given character with space. Else\r\n print space character.*\/\r\n if (m == n || n == rowsnumber - 1)\r\n printf(\"%c \", givencharacter);\r\n else\r\n printf(\" \");\r\n }\r\n \/\/ After the end of the inner for loop print the\r\n \/\/ Newline Character.\r\n printf(\"\\n\");\r\n }\r\n return 0;\r\n}<\/pre>\n
9-\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