192k views
4 votes
[10p] Modify the previous script such that only multiples of 3 or 5 are considered in the sum, (e.g., 3,5,6,9,10,12,15 for N=17 ).

1 Answer

1 vote

Final answer:

To modify the previous script to consider only multiples of 3 or 5, you can add an if statement inside the loop that checks if the current number is divisible by 3 or 5. If it is, you can add it to the sum.

Step-by-step explanation:

To modify the previous script to consider only multiples of 3 or 5, you can add an if statement inside the loop that checks if the current number is divisible by 3 or 5. If it is, you can add it to the sum. Here is the modified script:



sum = 0

for i in range(1, N+1):
if i % 3 == 0 or i % 5 == 0:
sum += i

print(sum)



For example, if N = 17, the multiples of 3 or 5 are 3, 5, 6, 9, 10, 12, and 15. The sum of these numbers is 60.

User Bennettaur
by
7.3k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.