Robotics & Artificial Intelligence

Write a function that returns the sum of multiples of 3 and 5 between 0 and limit (parameter). For example, if the limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.

Python Functions

1 Like

Answer

def sum_multiples(limit):
    s = 0
    for i in range(0, limit + 1):
        if i % 3 == 0 or i % 5 == 0:
            s = s + i
    return s

limit = int(input("Enter limit: "))
result = sum_multiples(limit)
print(result)

Output

Enter limit: 20
98

Answered By

2 Likes


Related Questions