{"id":12279,"date":"2021-09-30T15:00:05","date_gmt":"2021-09-30T09:30:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=12279"},"modified":"2021-11-22T18:34:30","modified_gmt":"2021-11-22T13:04:30","slug":"python-program-to-print-alternate-numbers-pattern-using-while-loop","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-alternate-numbers-pattern-using-while-loop\/","title":{"rendered":"Python Program to Print Alternate Numbers Pattern using While Loop"},"content":{"rendered":"
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list<\/a> is mandatory.<\/p>\n Given the number of rows, the task is to Print Alternate Numbers Pattern using While Loop 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 Alternate Numbers Pattern using While Loop 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 Related Programs<\/strong>:<\/p>\n Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory. Given the number of rows, the task is to Print Alternate Numbers Pattern using While Loop in C, C++, and Python. Examples: Example1: Input: Given number of rows = 9 Output: …<\/p>\nGiven number of rows = 9<\/pre>\n
1 \r\n3 3 \r\n5 5 5 \r\n7 7 7 7 \r\n9 9 9 9 9 \r\n11 11 11 11 11 11 \r\n13 13 13 13 13 13 13 \r\n15 15 15 15 15 15 15 15 \r\n17 17 17 17 17 17 17 17 17<\/pre>\n
Given number of rows = 7<\/pre>\n
1 \r\n3 3 \r\n5 5 5 \r\n7 7 7 7 \r\n9 9 9 9 9 \r\n11 11 11 11 11 11 \r\n13 13 13 13 13 13 13<\/pre>\n
Program to Print Alternate Numbers Pattern using While Loop in C, C++, and Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the number of rows as static input and store it in a variable.\r\nnumberrows = 10\r\n# Take a variable(say f ) and initialize it 1.\r\nf = 1\r\n# Loop till f is less than or equal to the number of rows using While Loop.\r\nwhile(f <= numberrows):\r\n # Take a variable(say g ) and initialize it 1.\r\n g = 1\r\n # Loop till g is is less than or equal to f\r\n # i.e g<=f using Another While Loop(Nested While Loop).\r\n while g <= f:\r\n # Inside the inner While Loop print the value of 2*f-1 with space.\r\n print((f * 2 - 1), end=\" \")\r\n # Increment the value of g by 1.\r\n g = g + 1\r\n # After the end of the inner while loop increment the value of variable f\u00a0by 1.\r\n f = f + 1\r\n # Print the Newline character.\r\n print()\r\n<\/pre>\n
1 \r\n3 3 \r\n5 5 5 \r\n7 7 7 7 \r\n9 9 9 9 9 \r\n11 11 11 11 11 11 \r\n13 13 13 13 13 13 13 \r\n15 15 15 15 15 15 15 15 \r\n17 17 17 17 17 17 17 17 17 \r\n19 19 19 19 19 19 19 19 19 19<\/pre>\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n \/\/ Give the number of rows as static input and store it\r\n \/\/ in a variable.\r\n int numberrows = 7;\r\n\r\n \/\/ Take a variable(say f ) and initialize it 1.\r\n int f = 1;\r\n \/\/ Loop till f is less than or equal to the number of\r\n \/\/ rows using While Loop.\r\n while (f <= numberrows) {\r\n \/\/ Take a variable(say g ) and initialize it 1.\r\n int g = 1;\r\n \/\/ Loop till g is is less than or equal to f\r\n \/\/ i.e g<=f using Another While Loop(Nested While\r\n \/\/ Loop).\r\n while (g <= f) {\r\n \/\/ Inside the inner While Loop print the value\r\n \/\/ of 2*f-1 with space.\r\n cout << (f * 2 - 1) << \" \";\r\n \/\/ Increment the value of g by 1.\r\n g = g + 1;\r\n }\r\n \/\/ After the end of the inner while loop increment\r\n \/\/ the value of variable f\u00a0by 1.\r\n f = f + 1;\r\n \/\/ Print the Newline character.\r\n cout << endl;\r\n }\r\n\r\n return 0;\r\n}<\/pre>\n
1 \r\n3 3 \r\n5 5 5 \r\n7 7 7 7 \r\n9 9 9 9 9 \r\n11 11 11 11 11 11 \r\n13 13 13 13 13 13 13<\/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 numberrows = 7;\r\n\r\n \/\/ Take a variable(say f ) and initialize it 1.\r\n int f = 1;\r\n \/\/ Loop till f is less than or equal to the number of\r\n \/\/ rows using While Loop.\r\n while (f <= numberrows) {\r\n \/\/ Take a variable(say g ) and initialize it 1.\r\n int g = 1;\r\n \/\/ Loop till g is is less than or equal to f\r\n \/\/ i.e g<=f using Another While Loop(Nested While\r\n \/\/ Loop).\r\n while (g <= f) {\r\n \/\/ Inside the inner While Loop print the value\r\n \/\/ of 2*f-1 with space.\r\n printf(\"%d \", (f * 2 - 1));\r\n \/\/ Increment the value of g by 1.\r\n g = g + 1;\r\n }\r\n \/\/ After the end of the inner while loop increment\r\n \/\/ the value of variable f\u00a0by 1.\r\n f = f + 1;\r\n \/\/ Print the Newline character.\r\n printf(\"\\n\");\r\n }\r\n return 0;\r\n}<\/pre>\n
1 \r\n3 3 \r\n5 5 5 \r\n7 7 7 7 \r\n9 9 9 9 9 \r\n11 11 11 11 11 11 \r\n13 13 13 13 13 13 13<\/pre>\n
Method #2: Using While Loop (User Input)<\/h3>\n
\n
# Give the number of rows as user input using int(input()) and store it in a variable.\r\nnumberrows = int(input('Enter some random number of rows = '))\r\n# Take a variable(say f ) and initialize it 1.\r\nf = 1\r\n# Loop till f is less than or equal to the number of rows using While Loop.\r\nwhile(f <= numberrows):\r\n # Take a variable(say g ) and initialize it 1.\r\n g = 1\r\n # Loop till g is is less than or equal to f\r\n # i.e g<=f using Another While Loop(Nested While Loop).\r\n while g <= f:\r\n # Inside the inner While Loop print the value of 2*f-1 with space.\r\n print((f * 2 - 1), end=\" \")\r\n # Increment the value of g by 1.\r\n g = g + 1\r\n # After the end of the inner while loop increment the value of variable f\u00a0by 1.\r\n f = f + 1\r\n # Print the Newline character.\r\n print()\r\n<\/pre>\n
Enter some random number of rows = 10\r\n1 \r\n3 3 \r\n5 5 5 \r\n7 7 7 7 \r\n9 9 9 9 9 \r\n11 11 11 11 11 11 \r\n13 13 13 13 13 13 13 \r\n15 15 15 15 15 15 15 15 \r\n17 17 17 17 17 17 17 17 17 \r\n19 19 19 19 19 19 19 19 19 19<\/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 user input using\r\n \/\/ int(input()) and store it in a variable.\r\n int numberrows;\r\n cin >> numberrows;\r\n \/\/ Take a variable(say f ) and initialize it 1.\r\n int f = 1;\r\n \/\/ Loop till f is less than or equal to the number of\r\n \/\/ rows using While Loop.\r\n while (f <= numberrows) {\r\n \/\/ Take a variable(say g ) and initialize it 1.\r\n int g = 1;\r\n \/\/ Loop till g is is less than or equal to f\r\n \/\/ i.e g<=f using Another While Loop(Nested While\r\n \/\/ Loop).\r\n while (g <= f) {\r\n \/\/ Inside the inner While Loop print the value\r\n \/\/ of 2*f-1 with space.\r\n cout << (f * 2 - 1) << \" \";\r\n \/\/ Increment the value of g by 1.\r\n g = g + 1;\r\n }\r\n \/\/ After the end of the inner while loop increment\r\n \/\/ the value of variable f\u00a0by 1.\r\n f = f + 1;\r\n \/\/ Print the Newline character.\r\n cout << endl;\r\n }\r\n return 0;\r\n}<\/pre>\n
8\r\n1 \r\n3 3 \r\n5 5 5 \r\n7 7 7 7 \r\n9 9 9 9 9 \r\n11 11 11 11 11 11 \r\n13 13 13 13 13 13 13 \r\n15 15 15 15 15 15 15 15 \r\n<\/pre>\n
#include <stdio.h>\r\n\r\nint main()\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 numberrows;\r\n scanf(\"%d\", &numberrows);\r\n \/\/ Take a variable(say f ) and initialize it 1.\r\n int f = 1;\r\n \/\/ Loop till f is less than or equal to the number of\r\n \/\/ rows using While Loop.\r\n while (f <= numberrows) {\r\n \/\/ Take a variable(say g ) and initialize it 1.\r\n int g = 1;\r\n \/\/ Loop till g is is less than or equal to f\r\n \/\/ i.e g<=f using Another While Loop(Nested While\r\n \/\/ Loop).\r\n while (g <= f) {\r\n \/\/ Inside the inner While Loop print the value\r\n \/\/ of 2*f-1 with space.\r\n printf(\"%d \", (f * 2 - 1));\r\n \/\/ Increment the value of g by 1.\r\n g = g + 1;\r\n }\r\n \/\/ After the end of the inner while loop increment\r\n \/\/ the value of variable f\u00a0by 1.\r\n f = f + 1;\r\n \/\/ Print the Newline character.\r\n printf(\"\\n\");\r\n }\r\n return 0;\r\n}<\/pre>\n
9\r\n1 \r\n3 3 \r\n5 5 5 \r\n7 7 7 7 \r\n9 9 9 9 9 \r\n11 11 11 11 11 11 \r\n13 13 13 13 13 13 13 \r\n15 15 15 15 15 15 15 15 \r\n17 17 17 17 17 17 17 17 17 \r\n\r\n<\/pre>\n
\n
\n