{"id":12265,"date":"2021-09-30T15:00:34","date_gmt":"2021-09-30T09:30:34","guid":{"rendered":"https:\/\/python-programs.com\/?p=12265"},"modified":"2021-11-22T18:34:27","modified_gmt":"2021-11-22T13:04:27","slug":"python-program-to-print-a-unique-pyramid-pattern-of-digits","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-a-unique-pyramid-pattern-of-digits\/","title":{"rendered":"Python Program to Print a Unique Pyramid Pattern of Digits"},"content":{"rendered":"
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list<\/a> available.<\/p>\n Given the number of rows, the task is to Print a Unique Pyramid Pattern of Digits 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 a Unique Pyramid Pattern of Digits 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 number of rows as user input using int(input()) and store it in a variable.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2) C++ Implementation<\/strong><\/p>\n Give the number of rows as user input using cin and store it in a variable.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3) C Implementation<\/strong><\/p>\n Give the number of rows as user input using scanf and store it in a variable.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available. Given the number of rows, the task is to Print a Unique Pyramid Pattern of Digits in C, C++, and Python Examples: Example1: …<\/p>\nGiven number of rows = 8<\/pre>\n
1 \r\n1 2 1 \r\n1 2 3 2 1 \r\n1 2 3 4 3 2 1 \r\n1 2 3 4 5 4 3 2 1 \r\n1 2 3 4 5 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 6 5 4 3 2 1<\/pre>\n
Given number of rows = 7<\/pre>\n
1 \r\n1 2 1 \r\n1 2 3 2 1 \r\n1 2 3 4 3 2 1 \r\n1 2 3 4 5 4 3 2 1 \r\n1 2 3 4 5 6 5 4 3 2 1<\/pre>\n
Program to Print a Unique Pyramid Pattern of Digits in C, C++, and Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number of rows as static input and store it in a variable.\r\nnumberOfRows = 8\r\n# Loop from 1 to the number of rows using For loop.\r\nfor m in range(1, numberOfRows + 1):\r\n # Loop from 1 to m-1 using another For loop(Nested For loop)\r\n # where m is the iterator value of the parent For loop.\r\n for n in range(1, m - 1):\r\n # Print the inner loop iterator value with space.\r\n print(n, end=\" \")\r\n # Loop from m-1 to 0 in decreasing order using another For loop(Nested For loop)\r\n # where m is the iterator value of the parent For loop.\r\n for n in range(m - 1, 0, -1):\r\n # Print the inner loop iterator value with space.\r\n print(n, end=\" \")\r\n # Print the Newline character after the end of the inner loop.\r\n print()\r\n<\/pre>\n
1 \r\n1 2 1 \r\n1 2 3 2 1 \r\n1 2 3 4 3 2 1 \r\n1 2 3 4 5 4 3 2 1 \r\n1 2 3 4 5 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 6 5 4 3 2 1<\/pre>\n
#include <iostream>\r\n#include <math.h>\r\nusing namespace std;\r\nint main()\r\n{\r\n\r\n \/\/ Give the number of rows as static input and store it\r\n \/\/ in a variable.\r\n int numberOfRows = 8;\r\n \/\/ Loop from 1 to the number of rows using For loop.\r\n\r\n for (int m = 1; m <= numberOfRows; m++) {\r\n \/\/ Loop from 1 to m-1 using another For loop(Nested\r\n \/\/ For loop)\r\n \/\/ where m is the iterator value of the parent For\r\n \/\/ loop.\r\n for (int n = 1; n < m - 1; n++) {\r\n \/\/ Print the inner loop iterator value with\r\n \/\/ space.\r\n cout << n << \" \";\r\n }\r\n \/\/ Loop from m-1 to 0 in decreasing order using\r\n \/\/ another For loop(Nested For loop) where m is the\r\n \/\/ iterator value of the parent For loop.\r\n for (int n = m - 1; n > 0; n--) {\r\n \/\/ Print the inner loop iterator value with\r\n \/\/ space.\r\n cout << n << \" \";\r\n }\r\n \/\/ Print the Newline character after the end of the\r\n \/\/ inner loop.\r\n cout << endl;\r\n }\r\n\r\n return 0;\r\n}<\/pre>\n
1 \r\n1 2 1 \r\n1 2 3 2 1 \r\n1 2 3 4 3 2 1 \r\n1 2 3 4 5 4 3 2 1 \r\n1 2 3 4 5 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 6 5 4 3 2 1<\/pre>\n
#include <stdio.h>\r\n\r\nint main()\r\n{\r\n\r\n \/\/ Give the number of rows as static input and store it\r\n \/\/ in a variable.\r\n int numberOfRows = 7;\r\n \/\/ Loop from 1 to the number of rows using For loop.\r\n\r\n for (int m = 1; m <= numberOfRows; m++) {\r\n \/\/ Loop from 1 to m-1 using another For loop(Nested\r\n \/\/ For loop) where m is the iterator value of the\r\n \/\/ parent For loop.\r\n for (int n = 1; n < m - 1; n++) {\r\n \/\/ Print the inner loop iterator value with\r\n \/\/ space.\r\n printf(\"%d \", n);\r\n }\r\n \/\/ Loop from m-1 to 0 in decreasing order using\r\n \/\/ another For loop(Nested For loop) where m is the\r\n \/\/ iterator value of the parent For loop.\r\n for (int n = m - 1; n > 0; n--) {\r\n \/\/ Print the inner loop iterator value with\r\n \/\/ space.\r\n printf(\"%d \", n);\r\n }\r\n \/\/ Print the Newline character after the end of the\r\n \/\/ inner loop.\r\n printf(\"\\n\");\r\n }\r\n return 0;\r\n}<\/pre>\n
1 \r\n1 2 1 \r\n1 2 3 2 1 \r\n1 2 3 4 3 2 1 \r\n1 2 3 4 5 4 3 2 1 \r\n1 2 3 4 5 6 5 4 3 2 1<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the number of rows as user input using int(input()) and store it in a variable.\r\nnumberOfRows = int(input('Enter some random number of rows = '))\r\n# Loop from 1 to the number of rows using For loop.\r\nfor m in range(1, numberOfRows + 1):\r\n # Loop from 1 to m-1 using another For loop(Nested For loop)\r\n # where m is the iterator value of the parent For loop.\r\n for n in range(1, m - 1):\r\n # Print the inner loop iterator value with space.\r\n print(n, end=\" \")\r\n # Loop from m-1 to 0 in decreasing order using another For loop(Nested For loop)\r\n # where m is the iterator value of the parent For loop.\r\n for n in range(m - 1, 0, -1):\r\n # Print the inner loop iterator value with space.\r\n print(n, end=\" \")\r\n # Print the Newline character after the end of the inner loop.\r\n print()\r\n<\/pre>\n
Enter some random number of rows = 17\r\n1 \r\n1 2 1 \r\n1 2 3 2 1 \r\n1 2 3 4 3 2 1 \r\n1 2 3 4 5 4 3 2 1 \r\n1 2 3 4 5 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 13 12 11 10 9 8 7 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 \r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1<\/pre>\n
#include <iostream>\r\n#include <math.h>\r\nusing namespace std;\r\nint main()\r\n{\r\n\r\n \/\/ Give the number of rows as user input using\r\n \/\/ int(input()) and store it in a variable.\r\n int numberOfRows;\r\n cin >> numberOfRows;\r\n \/\/ Loop from 1 to the number of rows using For loop.\r\n\r\n for (int m = 1; m <= numberOfRows; m++) {\r\n \/\/ Loop from 1 to m-1 using another For loop(Nested\r\n \/\/ For loop)\r\n \/\/ where m is the iterator value of the parent For\r\n \/\/ loop.\r\n for (int n = 1; n < m - 1; n++) {\r\n \/\/ Print the inner loop iterator value with\r\n \/\/ space.\r\n cout << n << \" \";\r\n }\r\n \/\/ Loop from m-1 to 0 in decreasing order using\r\n \/\/ another For loop(Nested For loop) where m is the\r\n \/\/ iterator value of the parent For loop.\r\n for (int n = m - 1; n > 0; n--) {\r\n \/\/ Print the inner loop iterator value with\r\n \/\/ space.\r\n cout << n << \" \";\r\n }\r\n \/\/ Print the Newline character after the end of the\r\n \/\/ inner loop.\r\n cout << endl;\r\n }\r\n\r\n return 0;\r\n}<\/pre>\n
5\r\n1 \r\n1 2 1 \r\n1 2 3 2 1 \r\n1 2 3 4 3 2 1 \r\n\r\n<\/pre>\n
#include <stdio.h>\r\nint main()\r\n{\r\n\r\n \/\/ Give the number of rows as user input using scanf and\r\n \/\/ store it in a variable.\r\n int numberOfRows;\r\n scanf(\"%d\", &numberOfRows);\r\n \/\/ Loop from 1 to the number of rows using For loop.\r\n\r\n for (int m = 1; m <= numberOfRows; m++) {\r\n \/\/ Loop from 1 to m-1 using another For loop(Nested\r\n \/\/ For loop) where m is the iterator value of the\r\n \/\/ parent For loop.\r\n for (int n = 1; n < m - 1; n++) {\r\n \/\/ Print the inner loop iterator value with\r\n \/\/ space.\r\n printf(\"%d \", n);\r\n }\r\n \/\/ Loop from m-1 to 0 in decreasing order using\r\n \/\/ another For loop(Nested For loop) where m is the\r\n \/\/ iterator value of the parent For loop.\r\n for (int n = m - 1; n > 0; n--) {\r\n \/\/ Print the inner loop iterator value with\r\n \/\/ space.\r\n printf(\"%d \", n);\r\n }\r\n \/\/ Print the Newline character after the end of the\r\n \/\/ inner loop.\r\n printf(\"\\n\");\r\n }\r\n return 0;\r\n}<\/pre>\n
6\r\n1 \r\n1 2 1 \r\n1 2 3 2 1 \r\n1 2 3 4 3 2 1 \r\n1 2 3 4 5 4 3 2 1<\/pre>\n
\n