{"id":8734,"date":"2023-11-04T11:11:19","date_gmt":"2023-11-04T05:41:19","guid":{"rendered":"https:\/\/python-programs.com\/?p=8734"},"modified":"2023-11-10T12:16:53","modified_gmt":"2023-11-10T06:46:53","slug":"python-program-to-print-all-numbers-in-a-range-divisible-by-a-given-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-all-numbers-in-a-range-divisible-by-a-given-number\/","title":{"rendered":"Python Program to Print all Numbers in a Range Divisible by a Given Number"},"content":{"rendered":"
Given three integers, the task is to print all values in the given range that are divisible by the third number, where the first number specifies the lower limit and the second number specifies the upper limit.<\/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 Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to print all the numbers in the given range which are divisible by the given number some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Given three integers, the task is to print all values in the given range that are divisible by the third number, where the first number specifies the lower limit and the second number specifies the upper limit. Examples: Example1: Input: lower limit = 1 upper limit = 263 given number = 5 Output: The numbers …<\/p>\nlower limit = 1 \r\nupper limit = 263 \r\ngiven number = 5<\/pre>\n
The numbers which are divisible by 5 from 17 to 263 are:\r\n20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 \r\n170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260<\/pre>\n
Enter the lower limit = 37 \r\nEnter the upper limit = 217 \r\nEnter the given number = 3<\/pre>\n
The numbers which are divisible by 3 from 37 to 217 are:\r\n39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108 111\r\n114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159 162 165 168 \r\n171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216<\/pre>\n
Enter the lower limit = 128\r\nEnter the upper limit = 659\r\nEnter the given number = 7<\/pre>\n
The numbers which are divisible by 7 from 128 to 659 are:\r\n133 140 147 154 161 168 175 182 189 196 203 210 217 224 231 238 245 252 259 266 273 280 287 294 301 308 \r\n315 322 329 336 343 350 357 364 371 378 385 392 399 406 413 420 427 434 441 448 455 462 469 476 483 490 \r\n497 504 511 518 525 532 539 546 553 560 567 574 581 588 595 602 609 616 623 630 637 644 651 658<\/pre>\n
Program to Print all Numbers in a Range Divisible by a Given Number in Python<\/h2>\n
\n
Method #1:Using for loop(Static Input)<\/h3>\n
\n
# given lower limit\r\nlower_limit = 17\r\n# given upper limit\r\nupper_limit = 263\r\n# given number\r\nnumb = 5\r\nprint(\"The numbers which are divisible by\", numb,\r\n \"from\", lower_limit, \"to\", upper_limit, \"are:\")\r\n# Using for loop, loop from lower limit to upper limit\r\nfor val in range(lower_limit, upper_limit):\r\n # Using an if conditional statement, determine whether\r\n # the iterator value is divisible by the given number.\r\n if(val % numb == 0):\r\n print(val, end=\" \")\r\n<\/pre>\n
The numbers which are divisible by 5 from 17 to 263 are:\r\n20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260<\/pre>\n
Method #2:Using for loop(User Input)<\/h3>\n
\n
# Scanning the lower limit\r\nlower_limit = int(input(\"Enter the lower limit = \"))\r\n# Scanning the upper limit\r\nupper_limit = int(input(\"Enter the upper limit = \"))\r\n# Scanning the given number\r\nnumb = int(input(\"Enter the given number = \"))\r\nprint(\"The numbers which are divisible by\", numb,\r\n \"from\", lower_limit, \"to\", upper_limit, \"are:\")\r\n# Using for loop, loop from lower limit to upper limit\r\nfor val in range(lower_limit, upper_limit):\r\n # Using an if conditional statement, determine whether\r\n # the iterator value is divisible by the given number.\r\n if(val % numb == 0):\r\n print(val, end=\" \")\r\n<\/pre>\n
Enter the lower limit = 37\r\nEnter the upper limit = 217\r\nEnter the given number = 3\r\nThe numbers which are divisible by 3 from 37 to 217 are:\r\n39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216<\/pre>\n
\n
\n
Method #3:Using While loop (User Input)<\/h3>\n
\n
# Scanning the lower limit\r\nlower_limit = int(input(\"Enter the lower limit = \"))\r\n# Scanning the upper limit\r\nupper_limit = int(input(\"Enter the upper limit = \"))\r\n# Scanning the given number\r\nnumb = int(input(\"Enter the given number = \"))\r\n# Take a variable tempo and initialize it with lower limit.\r\ntempo = lower_limit\r\nprint(\"The numbers which are divisible by\", numb,\r\n \"from\", lower_limit, \"to\", upper_limit, \"are:\")\r\n# Loop till tempo is less than upper limit using while loop.\r\nwhile(tempo < upper_limit):\r\n # Using an if conditional statement, determine whether\r\n # the tempo value is divisible by the given number.\r\n if(tempo % numb == 0):\r\n print(tempo, end=\" \")\r\n # Increment the tempo by 1\r\n tempo = tempo+1\r\n<\/pre>\n
Enter the lower limit = 128\r\nEnter the upper limit = 659\r\nEnter the given number = 7\r\nThe numbers which are divisible by 7 from 128 to 659 are:\r\n133 140 147 154 161 168 175 182 189 196 203 210 217 224 231 238 245 252 259 266 273 280 287 294 301 308 315 322 329 336 343 350 357 364 371 378 385 392 399 406 413 420 427 434 441 448 455 462 469 476 483 490 497 504 511 518 525 532 539 546 553 560 567 574 581 588 595 602 609 616 623 630 637 644 651 658<\/pre>\n
\n