Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,923 questions

51,856 answers

573 users

How to find the sum of all the multiples of 3 or 5 below 10 in Python

3 Answers

0 votes
sum = 0
i = 3

while (i < 10) :
    if (i % 3 == 0 or i % 5 == 0) :
        print(i)
        sum += i
    i += 1
    
print("sum = " + str(sum))




'''
run:

3
5
6
9
sum = 23

'''

 



answered Oct 12, 2023 by avibootz
0 votes
sum = 0

for i in range(3,10):
    if (i % 3 == 0 or i % 5 == 0) :
        print(i)
        sum += i
    i += 1
    
print("sum = " + str(sum))




'''
run:

3
5
6
9
sum = 23

'''

 



answered Oct 12, 2023 by avibootz
0 votes
def Summ( n) :
    return int((n * (n + 1)) / 2)
         
below10 = 9
sum = 3 * Summ((int(below10 / 3))) + 5 * Summ((int(below10 / 5))) - (3 * 5) * Summ((int(below10 / 15)))
 
print("sum = " + str(sum))
 
 
 
 
'''
run:
 
sum = 23
 
'''

 



answered Oct 12, 2023 by avibootz
edited Oct 13, 2023 by avibootz

Related questions

2 answers 136 views
2 answers 156 views
2 answers 145 views
2 answers 118 views
2 answers 130 views
2 answers 107 views
...