216k views
4 votes
Write a program that prints the sum of all the numbers that are divisible by both 3 and 5 (e.g. 15,30,45 ) between 0 and 100 .

User Dobeerman
by
8.1k points

1 Answer

4 votes

Final answer:

The sum of numbers divisible by both 3 and 5 between 0 and 100 is 315.

Step-by-step explanation:

Simple Python program to find and sum the numbers divisible by both 3 and 5 within the given range. The program iterates through each number from 0 to 100, checks if it is divisible by both 3 and 5, and if so, adds it to the total sum. visible by both 3 and 5 is printed.

# Python program to find the sum of numbers divisible by both 3 and 5

def find_sum():

total_sum = 0

for num in range(101):

if num % 3 == 0 and num % 5 == 0:

total_sum += num

return total_sum

# Calling the function to get the final result

result = find_sum()

# Displaying the final result

print("The sum of numbers divisible by both 3 and 5 between 0 and 100 is:", result)

The program iterates through numbers from 0 to 100 and checks for divisibility by both 3 and 5 using the modulo operator (%). If a number meets both conditions, it is added to the total sum. The final sum is then printed as the result.

The sum of numbers divisible by both 3 and 5 between 0 and 100 is found to be 315. The program effectively identifies and accumulates the relevant numbers, providing a clear and concise solution to the problem.

User Lliane
by
7.1k points