{"id":10864,"date":"2021-09-30T14:30:29","date_gmt":"2021-09-30T09:00:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=10864"},"modified":"2021-11-22T18:34:32","modified_gmt":"2021-11-22T13:04:32","slug":"python-program-to-print-1-and-0-in-alternative-rows","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-1-and-0-in-alternative-rows\/","title":{"rendered":"Python Program to Print 1 and 0 in Alternative Rows"},"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 and columns, the task is to print 1 and 0 in alternative rows 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 1 and 0 in alternative rows in C, C++, and Python.<\/p>\n If you look closely at the pattern, you’ll see that for all odd rows, 1 is printed and for all even rows, 0 is printed. As a<\/p>\n result, before displaying integers inside the inner loop, you must check for even-odd conditions. If the current row is<\/p>\n an odd number, print 1 otherwise, print 0.<\/p>\n The following is a step-by-step description of the logic used to print a 1, 0 number pattern at alternate rows.<\/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 It is the same as the python approach but just the change in syntax.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3) C Implementation<\/strong><\/p>\n It is the same as the python approach but just the change in syntax.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 1) C Implementation<\/strong><\/p>\n We just iterate till rownumbs and columumbs using a while loop.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2) C++ Implementation:<\/strong><\/p>\n We just iterate till rownumbs and columumbs using a while loop.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 3) Python Implementation<\/strong><\/p>\n We just iterate till rownumbs and columumbs using a while loop.<\/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 and columns, the task is to print 1 and 0 in alternative rows in C, C++, and Python. Examples: Example1: Input: given number of rows =4 …<\/p>\ngiven number of rows =4\r\ngiven number of columns=3<\/pre>\n
1 1 1 \r\n0 0 0 \r\n1 1 1 \r\n0 0 0<\/pre>\n
given number of rows =7\r\ngiven number of columns=15<\/pre>\n
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1<\/pre>\n
Program to Print 1 and 0 in Alternative Rows in C, C++, and Python<\/h2>\n
\n
Method #1: Using For loop<\/h3>\n
\n
# Give the number of rows and number of columns as static input.\r\n# Store them in two separate variables row numbers and column numbers.\r\nrownumbs = 15\r\ncolnumbs = 11\r\n# Run an outer loop from 1 to rows to iterate through the rows using For loop.\r\nfor m in range(1, rownumbs+1):\r\n # Iterate through the columns from 1 to cols using another For inner loop.\r\n for n in range(1, colnumbs+1):\r\n # Before printing any number, we must first check the condition inside the inner loop.\r\n # This means that for every odd row, 1 is displayed, and for every even row, 0 is displayed.\r\n # We check whether the row is odd or not using the if statement.\r\n # If it is true then print 1 else print 0.\r\n if(m % 2 == 1):\r\n print('1', end=' ')\r\n # If it is true then print 1 else print 0.\r\n else:\r\n print('0', end=' ')\r\n print()\r\n<\/pre>\n
1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1<\/pre>\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n \/\/ Give the number of rows and number of columns as\r\n \/\/ static input.\r\n \/\/ Store them in two separate variables row numbers and\r\n \/\/ column numbers.\r\n int rownumbs = 15;\r\n int colnumbs = 11;\r\n \/\/ Run an outer loop from 1 to rows to iterate through\r\n \/\/ the rows using For loop.\r\n for (int i = 1; i <= rownumbs; i++) {\r\n \/\/ Iterate through the columns from 1 to cols using\r\n \/\/ another For inner loop.\r\n for (int j = 1; j <= colnumbs; j++) {\r\n \/\/ Before printing any number, we must\r\n \/\/ first check the condition inside the\r\n \/\/ inner loop.\r\n \/\/ This means that for every odd row, 1 is\r\n \/\/ displayed, and for every even row,\r\n \/\/ 0 is displayed.\r\n \/\/ We check whether the row is odd or not using\r\n \/\/ the if statement.\r\n \/\/ If it is true then print 1 else print 0.\r\n\r\n if (i % 2 == 1)\r\n cout << \"1 \";\r\n \/\/ If it is true then print 1 else print 0.\r\n else\r\n cout << \"0 \";\r\n }\r\n cout << endl;\r\n }\r\n return 0;\r\n}<\/pre>\n
1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1<\/pre>\n
#include <stdio.h>\r\n\r\nint main(void)\r\n{\r\n \/\/ Give the number of rows and number of columns as\r\n \/\/ static input.\r\n \/\/ Store them in two separate variables row numbers and\r\n \/\/ column numbers.\r\n int rownumbs = 15;\r\n int colnumbs = 11;\r\n \/\/ Run an outer loop from 1 to rows to iterate through\r\n \/\/ the rows using For loop.\r\n for (int i = 1; i <= rownumbs; i++) {\r\n \/\/ Iterate through the columns from 1 to cols using\r\n \/\/ another For inner loop.\r\n for (int j = 1; j <= colnumbs;\r\n j++) { \/\/ Before printing any number, we must\r\n \/\/ first check the condition inside the\r\n \/\/ inner loop.\r\n \/\/ This means that for every odd row, 1 is\r\n \/\/ displayed, and for every even row, 0 is\r\n \/\/ displayed.\r\n \/*We check whether the row is odd or not using\r\n the if statement. If it is true then print 1\r\n else print 0.*\/\r\n if (i % 2 == 1)\r\n printf(\"1 \");\r\n \/\/ If it is true then print 1 else print 0.\r\n else\r\n printf(\"0 \");\r\n }\r\n printf(\"\\n\");\r\n }\r\n return 0;\r\n}<\/pre>\n
1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1<\/pre>\n
Method #2: Using while loop<\/h3>\n
#include <stdio.h>\r\n\r\nint main(void)\r\n{\r\n \/\/ Give the number of rows and number of columns as\r\n \/\/ static input.\r\n \/\/ Store them in two separate variables row numbers and\r\n \/\/ column numbers.\r\n int rownumbs = 4;\r\n int colnumbs = 3;\r\n \/\/ Run an outer loop from 1 to rows to iterate through\r\n \/\/ the rows using For loop.\r\n int temprow = 1;\r\n while (temprow <= rownumbs) {\r\n \/\/ Iterate through the columns from 1 to cols using\r\n \/\/ another For inner loop.\r\n int tempcol = 1;\r\n while (tempcol <= colnumbs) { \/\/ Before printing any\r\n \/\/ number, we must\r\n \/\/ first check the condition inside the\r\n \/\/ inner loop. This means that for every odd\r\n \/\/ row, 1 is displayed, and for every even row,\r\n \/\/ 0 is displayed. # We check whether the row is\r\n \/\/ odd or not using the if statement. # If it is\r\n \/\/ true then print 1 else print 0.\r\n if (temprow % 2 == 1)\r\n printf(\"1 \");\r\n \/\/ If it is true then print 1 else print 0.\r\n else\r\n printf(\"0 \");\r\n tempcol++;\r\n }\r\n temprow++;\r\n printf(\"\\n\");\r\n }\r\n return 0;\r\n}<\/pre>\n
1 1 1 \r\n0 0 0 \r\n1 1 1 \r\n0 0 0<\/pre>\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main(void)\r\n{\r\n \/\/ Give the number of rows and number of columns as\r\n \/\/ static input.\r\n \/\/ Store them in two separate variables row numbers and\r\n \/\/ column numbers.\r\n int rownumbs = 2;\r\n int colnumbs = 19;\r\n \/\/ Run an outer loop from 1 to rows to iterate through\r\n \/\/ the rows using For loop.\r\n int temprow = 1;\r\n while (temprow <= rownumbs) {\r\n \/\/ Iterate through the columns from 1 to cols using\r\n \/\/ another For inner loop.\r\n int tempcol = 1;\r\n while (tempcol <= colnumbs) { \/\/ Before printing any\r\n \/\/ number, we must\r\n \/\/ first check the condition inside the\r\n \/\/ inner loop. This means that for every odd\r\n \/\/ row, 1 is displayed, and for every even row,\r\n \/\/ 0 is displayed. # We check whether the row is\r\n \/\/ odd or not using the if statement. # If it is\r\n \/\/ true then print 1 else print 0.\r\n if (temprow % 2 == 1)\r\n cout << \"1 \";\r\n \/\/ If it is true then print 1 else print 0.\r\n else\r\n cout << \"0 \";\r\n tempcol++;\r\n }\r\n temprow++;\r\n cout << endl;\r\n }\r\n return 0;\r\n}<\/pre>\n
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0<\/pre>\n
# Give the number of rows and number of columns as static input.\r\n# Store them in two separate variables row numbers and column numbers.\r\nrownumbs = 6\r\ncolnumbs = 11\r\n# Run an outer loop from 1 to rows to iterate through the rows using For loop.\r\ntemprow = 1\r\nwhile(temprow <= rownumbs):\r\n # Iterate through the columns from 1 to cols using another For inner loop.\r\n tempcol = 1\r\n while(tempcol <= colnumbs):\r\n # Before printing any number, we must first check the condition inside the inner loop.\r\n # This means that for every odd row, 1 is displayed, and for every even row, 0 is displayed.\r\n # We check whether the row is odd or not using the if statement.\r\n if(temprow % 2 == 1):\r\n print('1', end=' ')\r\n # If it is true then print 1 else print 0.\r\n else:\r\n print('0', end=' ')\r\n tempcol += 1\r\n temprow += 1\r\n print()\r\n<\/pre>\n
1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0 \r\n1 1 1 1 1 1 1 1 1 1 1 \r\n0 0 0 0 0 0 0 0 0 0 0<\/pre>\n
\n