{"id":11892,"date":"2021-09-30T14:30:00","date_gmt":"2021-09-30T09:00:00","guid":{"rendered":"https:\/\/python-programs.com\/?p=11892"},"modified":"2021-11-22T18:34:34","modified_gmt":"2021-11-22T13:04:34","slug":"python-program-to-print-pyramid-star-pattern","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-pyramid-star-pattern\/","title":{"rendered":"Python Program to Print Pyramid Star Pattern"},"content":{"rendered":"
Are you a job seeker and trying to find simple java programs for Interview?<\/a> This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.<\/p>\n Given the number of rows of the pyramid, the task is to print the Pyramid 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 Pyramid 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 Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview. Given the number of rows of the pyramid, the task is to print the Pyramid Star pattern …<\/p>\nGiven number of rows of the pyramid\u00a0 =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 rows of the pyramid\u00a0 =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 Pyramid 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 pyramid as static input and store it in a variable.\r\npyRows = 10\r\n# Loop from the number of rows to 0 in decreasing order using For Loop.\r\n# Loop from 0 to the number of rows using For Loop.\r\nfor m in range(0, pyRows):\r\n # Loop from 0 to the number of rows - iterator value-1 of the parent For loop\\\r\n # using another Nested For loop(Inner For loop).\r\n for n in range(0, pyRows-m-1):\r\n # Print the space character in the inner For loop.\r\n print(end=' ')\r\n # Loop from 0 to the iterator value+1 of the parent For loop\r\n # using another Nested For loop(Inner For loop).\r\n\r\n for l in range(0, m+1):\r\n # Print the star character with a star character in the inner For loop.\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 number of rows of the pyramid as static\r\n \/\/ input and store it in a variable.\r\n int pyRows = 10;\r\n \/\/ Loop from 0 to the number of rows using For Loop.\r\n for (int m = 0; m < pyRows; m++) {\r\n \/\/ Loop from 0 to the number of rows - iterator value-1 of the parent For loop\\\r\n \/\/ using another Nested For loop(Inner For loop).\r\n for (int n = 0; n < pyRows - m - 1; n++) {\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n cout << \" \";\r\n }\r\n \/\/ Loop from 0 to the iterator value+1 of the parent\r\n \/\/ For loop\r\n \/\/ using another Nested For loop(Inner For loop).\r\n for (int l = 0; l < m + 1; l++) {\r\n \/\/ Print the star character with a space\r\n \/\/ character in the inner For loop.\r\n cout << \"* \";\r\n }\r\n\r\n \/\/ Print the Newline Character after the end of the\r\n \/\/ inner for loop.\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* * * * * * * * * *<\/pre>\n
#include <stdio.h>\r\n\r\nint main()\r\n{\r\n\r\n \/\/ Give the number of rows of the pyramid as static\r\n \/\/ input and store it in a variable.\r\n int pyRows = 10;\r\n \/\/ Loop from 0 to the number of rows using For Loop.\r\n for (int m = 0; m < pyRows; m++) {\r\n \/\/ Loop from 0 to the number of rows - iterator value-1 of the parent For loop\\\r\n \/\/ using another Nested For loop(Inner For loop).\r\n for (int n = 0; n < pyRows - m - 1; n++) {\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n printf(\" \");\r\n }\r\n \/\/ Loop from 0 to the iterator value+1 of the parent\r\n \/\/ For loop\r\n \/\/ using another Nested For loop(Inner For loop).\r\n for (int l = 0; l < m + 1; l++) {\r\n \/\/ Print the star character with a space\r\n \/\/ character in the inner For loop.\r\n printf(\"* \");\r\n }\r\n\r\n \/\/ Print the Newline Character after the end of the\r\n \/\/ inner for loop.\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* * * * * * * * * *<\/pre>\n
Method #2: Using For Loop (User Character)<\/h3>\n
\n
\n
# Give the number of rows of the pyramid as user input using int(input()) and store it in a variable.\r\npyRows = int(input(\r\n 'Enter some random number of rows of pyramid = '))\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, pyRows):\r\n # Loop from 0 to the number of rows - iterator value-1 of the parent For loop\\\r\n # using another Nested For loop(Inner For loop).\r\n for n in range(0, pyRows-m-1):\r\n # Print the space character in the inner For loop.\r\n print(end=' ')\r\n # Loop from 0 to the iterator value+1 of the parent For loop\r\n # using another Nested For loop(Inner For loop).\r\n\r\n for l in range(0, m+1):\r\n # Print the given character with a space character in the inner For loop.\r\n print(givencharacter, end=' ')\r\n print()\r\n<\/pre>\n
Enter some random number of rows of pyramid = 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^ ^ ^ ^ ^ ^ ^ ^ ^ ^<\/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 pyRows;\r\n char givencharacter;\r\n cout << \"Enter some random number of rows = \" << endl;\r\n cin >> pyRows;\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 < pyRows; m++) {\r\n \/\/ Loop from 0 to the number of rows - iterator value-1 of the parent For loop\\\r\n \/\/ using another Nested For loop(Inner For loop).\r\n for (int n = 0; n < pyRows - m - 1; n++) {\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n cout << \" \";\r\n }\r\n \/\/ Loop from 0 to the iterator value+1 of the parent\r\n \/\/ For loop\r\n \/\/ using another Nested For loop(Inner For loop).\r\n for (int l = 0; l < m + 1; l++) {\r\n \/\/ Print the given character with a space\r\n \/\/ character in the inner For loop.\r\n cout <<givencharacter<<\" \";\r\n }\r\n\r\n \/\/ Print the Newline Character after the end of the\r\n \/\/ inner for loop.\r\n cout << endl;\r\n }\r\n return 0;\r\n}<\/pre>\n
Enter some random number of rows of pyramid = \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^ ^ ^ ^ ^ ^ ^ ^ ^ ^<\/pre>\n
\n
#include <stdio.h>\r\n\r\nint main()\r\n{\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 pyRows;\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\", &pyRows);\r\n scanf(\"%c\", &givencharacter);\r\n \/\/ Loop from 0 to the number of rows using For Loop.\r\n for (int m = 0; m < pyRows; m++) {\r\n \/\/ Loop from 0 to the number of rows - iterator value-1 of the parent For loop\\\r\n \/\/ using another Nested For loop(Inner For loop).\r\n for (int n = 0; n < pyRows - m - 1; n++) {\r\n \/\/ Print the space character in the inner For\r\n \/\/ loop.\r\n printf(\" \");\r\n }\r\n \/\/ Loop from 0 to the iterator value+1 of the parent\r\n \/\/ For loop\r\n \/\/ using another Nested For loop(Inner For loop).\r\n for (int l = 0; l < m + 1; l++) {\r\n \/\/ Print the given character with a space\r\n \/\/ character in the inner For loop.\r\n printf(\"%c \", givencharacter);\r\n }\r\n\r\n \/\/ Print the Newline Character after the end of the\r\n \/\/ inner for loop.\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^ ^ ^ ^ ^ ^ ^ ^ ^ ^<\/pre>\n
\n