{"id":10878,"date":"2021-09-30T15:00:12","date_gmt":"2021-09-30T09:30:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=10878"},"modified":"2021-11-22T18:34:29","modified_gmt":"2021-11-22T13:04:29","slug":"python-program-to-print-square-number-pattern","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-square-number-pattern\/","title":{"rendered":"Python Program to Print Square Number 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 and a number, the task is to print a square number 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 square number 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 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 and a number, the task is to print a square number pattern in C, C++, and Python. Examples: Example1: Input: given …<\/p>\ngiven number of sides = 5\r\ngiven element of square =3<\/pre>\n
3 3 3 3 3 \r\n3 3 3 3 3 \r\n3 3 3 3 3 \r\n3 3 3 3 3 \r\n3 3 3 3 3<\/pre>\n
given number of sides = 7\r\ngiven element of square =9<\/pre>\n
9 9 9 9 9 9 9 \r\n9 9 9 9 9 9 9 \r\n9 9 9 9 9 9 9 \r\n9 9 9 9 9 9 9 \r\n9 9 9 9 9 9 9 \r\n9 9 9 9 9 9 9 \r\n9 9 9 9 9 9 9<\/pre>\n
Program to Print Square Number Pattern in C, C++, and Python<\/h2>\n
\n
Method #1: Using For loop (Element is of static input)<\/h3>\n
\n
# Give the number of sides of the square as static input and store it in a variable.\r\nsidesnum = 10\r\n# Using Nested For loops print the square pattern.\r\nfor m in range(sidesnum):\r\n for n in range(sidesnum):\r\n print('1', end=' ')\r\n print()\r\n<\/pre>\n
1 1 1 1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 1 1 1<\/pre>\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main(void)\r\n{\r\n \/\/ Give the number of sides of the square as static\r\n \/\/ input\r\n \/\/ and store it in a variable.\r\n int sidesnum = 7;\r\n \/\/ Using Nested For loops print the square pattern.\r\n for (int m = 1; m <= sidesnum; m++) {\r\n for (int n = 1; n <= sidesnum; n++) {\r\n cout << \"1 \";\r\n }\r\n cout << endl;\r\n }\r\n\r\n return 0;\r\n}<\/pre>\n
1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1<\/pre>\n
#include<stdio.h>\r\n \r\nint main()\r\n{\r\n \/\/ Give the number of sides of the square as static input\r\n \/\/ and store it in a variable.\r\n int sidesnum = 7, m, n;\r\n \/\/ Using Nested For loops print the square pattern.\r\n for(m = 0; m < sidesnum; m++)\r\n {\r\n \tfor(n = 0; n < sidesnum; n++)\r\n {\r\n \tprintf(\"1 \");\r\n }\r\n printf(\"\\n\");\r\n }\r\n return 0;\r\n}\r\n<\/pre>\n
1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 \r\n1 1 1 1 1 1 1 \r\n\r\n<\/pre>\n
Method #2: Using For loop (Element is of User input)<\/h3>\n
\n
\n
# Give the number of sides of the square as user input using int(input()) and store it in a variable.\r\n# Give the Element as user input using int(input()) and store it in another variable.\r\nsidesnum = int(input('Enter some random number of sides = '))\r\neleme = int(input('Enter some random element = '))\r\n# Using Nested For loops print the square pattern.\r\nfor m in range(sidesnum):\r\n for n in range(sidesnum):\r\n print(eleme, end=' ')\r\n print()\r\n<\/pre>\n
Enter some random number of sides = 11\r\nEnter some random element = 23\r\n23 23 23 23 23 23 23 23 23 23 23 \r\n23 23 23 23 23 23 23 23 23 23 23 \r\n23 23 23 23 23 23 23 23 23 23 23 \r\n23 23 23 23 23 23 23 23 23 23 23 \r\n23 23 23 23 23 23 23 23 23 23 23 \r\n23 23 23 23 23 23 23 23 23 23 23 \r\n23 23 23 23 23 23 23 23 23 23 23 \r\n23 23 23 23 23 23 23 23 23 23 23 \r\n23 23 23 23 23 23 23 23 23 23 23 \r\n23 23 23 23 23 23 23 23 23 23 23 \r\n23 23 23 23 23 23 23 23 23 23 23<\/pre>\n
\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main(void)\r\n{\r\n int sidesnum, eleme;\r\n \/\/ Give the number of sides of the square as user input\r\n \/\/ using cin and\r\n \/\/ store it in a variable.\r\n cin >> sidesnum;\r\n \/\/ Give the Element as user input using cin and store it\r\n \/\/ in another variable.\r\n cin >> eleme;\r\n\r\n \/\/ Using Nested For loops print the square pattern.\r\n for (int m = 1; m <= sidesnum; m++) {\r\n for (int n = 1; n <= sidesnum; n++) {\r\n cout << eleme << \" \";\r\n }\r\n cout << endl;\r\n }\r\n\r\n return 0;\r\n}<\/pre>\n
\n
#include<stdio.h>\r\n \r\nint main()\r\n{ int sidesnum, eleme;\r\n \/\/ Give the number of sides of the square as user input\r\n \/\/ using cin and\r\n \/\/ store it in a variable.\r\n scanf(\"%d\",&sidesnum);\r\n \/\/ Give the Element as user input using cin and store it\r\n \/\/ in another variable.\r\n scanf(\"%d\",&eleme);\r\n \/\/ Using Nested For loops print the square pattern.\r\n for (int m = 1; m <= sidesnum; m++) {\r\n for (int n = 1; n <= sidesnum; n++) {\r\n printf(\"%d \",eleme);\r\n }\r\n printf(\"\\n\");\r\n }\r\n\r\n}\r\n<\/pre>\n
22\r\n5\r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 \r\n5 5 5 5 5 5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