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

When it is required to print all the elements in a given range that is divisible by a specific number, a simple for loop can be used along with the modulus operator.

Below is a demonstration of the same −

Example

# Define range and divisor
lower_num = 10
upper_num = 50
div_num = 7

print(f"Numbers between {lower_num} and {upper_num} divisible by {div_num}:")
for i in range(lower_num, upper_num + 1):
    if i % div_num == 0:
        print(i)
Numbers between 10 and 50 divisible by 7:
14
21
28
35
42
49

How It Works

  • The range() function generates numbers from the lower limit to upper limit (inclusive)

  • The modulus operator % returns the remainder when dividing two numbers

  • When i % div_num == 0, it means the number is divisible with no remainder

  • All divisible numbers are printed to the screen

Alternative Method Using List Comprehension

You can also create a list of all divisible numbers in one line ?

# Using list comprehension
lower_num = 15
upper_num = 60
div_num = 8

divisible_numbers = [i for i in range(lower_num, upper_num + 1) if i % div_num == 0]
print(f"Numbers divisible by {div_num}: {divisible_numbers}")
Numbers divisible by 8: [16, 24, 32, 40, 48, 56]

Conclusion

Use the modulus operator % with a for loop to find numbers divisible by a given value. List comprehension provides a more concise approach for creating a collection of divisible numbers.

Updated on: 2026-03-25T19:05:52+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements