{"id":12213,"date":"2021-09-30T15:30:05","date_gmt":"2021-09-30T10:00:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=12213"},"modified":"2021-11-22T18:34:26","modified_gmt":"2021-11-22T13:04:26","slug":"python-program-to-print-square-pattern-with-numbers","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-square-pattern-with-numbers\/","title":{"rendered":"Python Program to Print Square Pattern with Numbers"},"content":{"rendered":"
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples<\/a> as it includes basic to advanced levels of concepts.<\/p>\n Given the number of rows, the task is to print Square Pattern with Numbers 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 Square Pattern with Numbers 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 The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts. Given the number of rows, the task is to print Square Pattern with Numbers in C, C++, and Python Examples: Example1: Input: Given number of rows = 9 …<\/p>\nGiven number of rows = 9<\/pre>\n
1 2 3 4 5 6 7 8 9 \r\n2 2 3 4 5 6 7 8 9 \r\n3 3 3 4 5 6 7 8 9 \r\n4 4 4 4 5 6 7 8 9 \r\n5 5 5 5 5 6 7 8 9 \r\n6 6 6 6 6 6 7 8 9 \r\n7 7 7 7 7 7 7 8 9 \r\n8 8 8 8 8 8 8 8 9 \r\n9 9 9 9 9 9 9 9 9<\/pre>\n
Given number of rows = 6<\/pre>\n
1 2 3 4 5 6 \r\n2 2 3 4 5 6 \r\n3 3 3 4 5 6 \r\n4 4 4 4 5 6 \r\n5 5 5 5 5 6 \r\n6 6 6 6 6 6<\/pre>\n
Program to Print Square Pattern with Numbers 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\nnumbrrows = 9\r\n# Loop from 1 to the number of rows using For loop.\r\nfor m in range(1, numbrrows+1):\r\n # Loop from 1 to the number of rows using another for loop(Nested For loop).\r\n for n in range(1, numbrrows+1):\r\n ''' Check if the iterator value of the inner For loop is less than or equal \r\n to the parent loop iterator value of the\r\n For Loop using the If conditional Statement.'''\r\n if(n <= m):\r\n # If it is true then print the iterator value of the parent For loop.\r\n print(m, end=' ')\r\n else:\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 2 3 4 5 6 7 8 9 \r\n2 2 3 4 5 6 7 8 9 \r\n3 3 3 4 5 6 7 8 9 \r\n4 4 4 4 5 6 7 8 9 \r\n5 5 5 5 5 6 7 8 9 \r\n6 6 6 6 6 6 7 8 9 \r\n7 7 7 7 7 7 7 8 9 \r\n8 8 8 8 8 8 8 8 9 \r\n9 9 9 9 9 9 9 9 9<\/pre>\n
#include <iostream>\r\nusing namespace std;\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 numbrrows = 5;\r\n \/\/ Loop from 1 to the number of rows using For loop.\r\n for (int m = 1; m <= numbrrows; m++) {\r\n \/\/ Loop from 1 to the number of rows using another\r\n \/\/ for loop(Nested For loop).\r\n for (int n = 1; n <= numbrrows; n++) {\r\n \/*Check if the iterator value of the inner For\r\n loop is less than or equal to the parent loop\r\n iterator value of the For Loop using the If\r\n conditional Statement.*\/\r\n if (n <= m)\r\n \/\/ If it is true then print the iterator\r\n \/\/ value of the parent For loop.\r\n cout << m << \" \";\r\n else\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 return 0;\r\n}<\/pre>\n
1 2 3 4 5 \r\n2 2 3 4 5 \r\n3 3 3 4 5 \r\n4 4 4 4 5 \r\n5 5 5 5 5<\/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 numbrrows = 3;\r\n \/\/ Loop from 1 to the number of rows using For loop.\r\n for (int m = 1; m <= numbrrows; m++) {\r\n \/\/ Loop from 1 to the number of rows using another\r\n \/\/ for loop(Nested For loop).\r\n for (int n = 1; n <= numbrrows; n++) {\r\n \/*Check if the iterator value of the inner For\r\n loop is less than or equal to the parent loop\r\n iterator value of the For Loop using the If\r\n conditional Statement.*\/\r\n if (n <= m)\r\n \/\/ If it is true then print the iterator\r\n \/\/ value of the parent For loop.\r\n printf(\"%d \", m);\r\n else\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 2 3 \r\n2 2 3 \r\n3 3 3<\/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\nnumbrrows = 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, numbrrows+1):\r\n # Loop from 1 to the number of rows using another for loop(Nested For loop).\r\n for n in range(1, numbrrows+1):\r\n ''' Check if the iterator value of the inner For loop is less than or equal \r\n to the parent loop iterator value of the\r\n For Loop using the If conditional Statement.'''\r\n if(n <= m):\r\n # If it is true then print the iterator value of the parent For loop.\r\n print(m, end=' ')\r\n else:\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 = 7\r\n1 2 3 4 5 6 7 \r\n2 2 3 4 5 6 7 \r\n3 3 3 4 5 6 7 \r\n4 4 4 4 5 6 7 \r\n5 5 5 5 5 6 7 \r\n6 6 6 6 6 6 7 \r\n7 7 7 7 7 7 7<\/pre>\n
#include <iostream>\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 numbrrows;\r\n cin >> numbrrows;\r\n \/\/ Loop from 1 to the number of rows using For loop.\r\n for (int m = 1; m <= numbrrows; m++) {\r\n \/\/ Loop from 1 to the number of rows using another\r\n \/\/ for loop(Nested For loop).\r\n for (int n = 1; n <= numbrrows; n++) {\r\n \/*Check if the iterator value of the inner For\r\n loop is less than or equal to the parent loop\r\n iterator value of the For Loop using the If\r\n conditional Statement.*\/\r\n if (n <= m)\r\n \/\/ If it is true then print the iterator\r\n \/\/ value of the parent For loop.\r\n cout << m << \" \";\r\n else\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 return 0;\r\n}<\/pre>\n
4\r\n1 2 3 4 \r\n2 2 3 4 \r\n3 3 3 4 \r\n4 4 4 4<\/pre>\n
#include <math.h>\r\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 numbrrows;\r\n scanf(\"%d\", &numbrrows);\r\n \/\/ Loop from 1 to the number of rows using For loop.\r\n for (int m = 1; m <= numbrrows; m++) {\r\n \/\/ Loop from 1 to the number of rows using another\r\n \/\/ for loop(Nested For loop).\r\n for (int n = 1; n <= numbrrows; n++) {\r\n \/*Check if the iterator value of the inner For\r\n loop is less than or equal to the parent loop\r\n iterator value of the For Loop using the If\r\n conditional Statement.*\/\r\n if (n <= m)\r\n \/\/ If it is true then print the iterator\r\n \/\/ value of the parent For loop.\r\n printf(\"%d \", m);\r\n else\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 2 3 4 5 6 \r\n2 2 3 4 5 6 \r\n3 3 3 4 5 6 \r\n4 4 4 4 5 6 \r\n5 5 5 5 5 6 \r\n6 6 6 6 6 6 \r\n\r\n<\/pre>\n
\n