Python Program to Print all Numbers in a Range Divisible by a Given Number

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 which are divisible by 5 from 17 to 263 are:
20 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

Example2:

Input:

Enter the lower limit = 37 
Enter the upper limit = 217 
Enter the given number = 3

Output:

The numbers which are divisible by 3 from 37 to 217 are:
39 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

Example3:

Input:

Enter the lower limit = 128
Enter the upper limit = 659
Enter the given number = 7

Output:

The numbers which are divisible by 7 from 128 to 659 are:
133 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

Program to Print all Numbers in a Range Divisible by a Given Number in Python

There are several ways to print all the numbers in the given range which are divisible by the given number some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1:Using for loop(Static Input)

Approach:

  • Give three numbers as static input.
  • Using for loop, loop from lower limit to upper limit.
  • Using an if conditional statement, determine whether the iterator value is divisible by the given number.
  • If it is divisible then print the iterator value.
  • Exit of program

Below is the implementation:

# given lower limit
lower_limit = 17
# given upper limit
upper_limit = 263
# given number
numb = 5
print("The numbers which are divisible by", numb,
      "from", lower_limit, "to", upper_limit, "are:")
# Using for loop, loop from lower limit to upper limit
for val in range(lower_limit, upper_limit):
    # Using an if conditional statement, determine whether
    # the iterator value is divisible by the given number.
    if(val % numb == 0):
        print(val, end=" ")

Output:

The numbers which are divisible by 5 from 17 to 263 are:
20 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

Method #2:Using for loop(User Input)

Approach:

  • Scan the lower limit, upper limit and the given number using int(input()).
  • Using for loop, loop from lower limit to upper limit.
  • Using an if conditional statement, determine whether the iterator value is divisible by the given number.
  • If it is divisible then print the iterator value.
  • Exit of program

Below is the implementation:

# Scanning the lower limit
lower_limit = int(input("Enter the lower limit = "))
# Scanning the upper limit
upper_limit = int(input("Enter the upper limit = "))
# Scanning the given number
numb = int(input("Enter the given number = "))
print("The numbers which are divisible by", numb,
      "from", lower_limit, "to", upper_limit, "are:")
# Using for loop, loop from lower limit to upper limit
for val in range(lower_limit, upper_limit):
    # Using an if conditional statement, determine whether
    # the iterator value is divisible by the given number.
    if(val % numb == 0):
        print(val, end=" ")

Output:

Enter the lower limit = 37
Enter the upper limit = 217
Enter the given number = 3
The numbers which are divisible by 3 from 37 to 217 are:
39 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

Explanation:

  • The user must enter the upper and lower range limits.
  • The user is next required to provide the number to be divided from the user.
  • The value of i is between the lower and upper bounds.
  • Whenever the remainder of an integer divided by numb equals zero, the i is printed.

Method #3:Using While loop (User Input)

Approach:

  • Scan the lower limit, upper limit and the given number using int(input()).
  • Take a variable tempo and initialize it with lower limit.
  • Loop till tempo is less than upper limit using while loop.
  • Using an if conditional statement, determine whether the tempo value is divisible by the given number.
  • If it is divisible then print the iterator value.
  • Increment the tempo by 1
  • Exit of program

Below is the implementation:

# Scanning the lower limit
lower_limit = int(input("Enter the lower limit = "))
# Scanning the upper limit
upper_limit = int(input("Enter the upper limit = "))
# Scanning the given number
numb = int(input("Enter the given number = "))
# Take a variable tempo and initialize it with lower limit.
tempo = lower_limit
print("The numbers which are divisible by", numb,
      "from", lower_limit, "to", upper_limit, "are:")
# Loop till tempo is less than upper limit using while loop.
while(tempo < upper_limit):
    # Using an if conditional statement, determine whether
    # the tempo value is divisible by the given number.
    if(tempo % numb == 0):
        print(tempo, end=" ")
    # Increment the tempo by 1
    tempo = tempo+1

Output:

Enter the lower limit = 128
Enter the upper limit = 659
Enter the given number = 7
The numbers which are divisible by 7 from 128 to 659 are:
133 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

Related Programs:

Leave a Comment