Python program to print all the numbers divisible by 3 and 5 for a given number

This Python program finds all numbers divisible by both 3 and 5 within a given range. Numbers divisible by both 3 and 5 are also divisible by their least common multiple (LCM), which is 15.

Method 1: Using Modulo Operator with AND Condition

We can check if a number is divisible by both 3 and 5 using the modulo operator ?

lower = int(input("Enter lower range limit: "))
upper = int(input("Enter upper range limit: "))

print(f"Numbers divisible by both 3 and 5 between {lower} and {upper}:")
for i in range(lower, upper + 1):
    if (i % 3 == 0) and (i % 5 == 0):
        print(i)
Enter lower range limit: 0
Enter upper range limit: 99
Numbers divisible by both 3 and 5 between 0 and 99:
0
15
30
45
60
75
90

Method 2: Using LCM (More Efficient)

Since numbers divisible by both 3 and 5 are divisible by 15, we can directly check divisibility by 15 ?

lower = int(input("Enter lower range limit: "))
upper = int(input("Enter upper range limit: "))

print(f"Numbers divisible by both 3 and 5 between {lower} and {upper}:")
for i in range(lower, upper + 1):
    if i % 15 == 0:
        print(i)
Enter lower range limit: 50
Enter upper range limit: 150
Numbers divisible by both 3 and 5 between 50 and 150:
60
75
90
105
120
135
150

Method 3: Using List Comprehension

A more Pythonic approach using list comprehension ?

lower = 1
upper = 100

divisible_by_3_and_5 = [i for i in range(lower, upper + 1) if i % 15 == 0]
print("Numbers divisible by both 3 and 5:")
print(divisible_by_3_and_5)
Numbers divisible by both 3 and 5:
[15, 30, 45, 60, 75, 90]

Generic Function for Any Two Numbers

Here's a flexible function that works for any two numbers ?

import math

def find_divisible_by_both(lower, upper, num1, num2):
    """Find numbers divisible by both num1 and num2 in given range"""
    lcm = (num1 * num2) // math.gcd(num1, num2)
    result = []
    
    for i in range(lower, upper + 1):
        if i % lcm == 0:
            result.append(i)
    
    return result

# Example usage
numbers = find_divisible_by_both(1, 100, 3, 5)
print("Numbers divisible by both 3 and 5:")
print(numbers)

# Another example with different numbers
numbers2 = find_divisible_by_both(1, 50, 4, 6)
print("\nNumbers divisible by both 4 and 6:")
print(numbers2)
Numbers divisible by both 3 and 5:
[15, 30, 45, 60, 75, 90]

Numbers divisible by both 4 and 6:
[12, 24, 36, 48]

Key Points

  • Use and operator instead of & for logical operations

  • Method 2 (using LCM) is more efficient as it performs fewer calculations

  • For 3 and 5, the LCM is 15, so checking i % 15 == 0 is sufficient

  • List comprehension provides a concise way to generate the results

Conclusion

The most efficient approach is checking divisibility by the LCM (15 for numbers 3 and 5). This reduces the number of operations compared to checking both conditions separately. Use the generic function when working with different pairs of numbers.

Updated on: 2026-03-25T05:17:18+05:30

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements