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

后端 未结 18 1159
萌比男神i
萌比男神i 2020-12-29 12:10

Not sure if I should\'ve posted this on math.stackexchange instead, but it includes more programming so I posted it here.

The question seems really simple, but I\'ve

18条回答
  •  太阳男子
    2020-12-29 12:39

    here is my solution:

    for n in range(100):
        if n % 5==0:
            if n % 3==0:
            print n, "Multiple of both 3 and 5"    #if the number is a multiple of 5, is it a multiple of 3? if yes it has has both.
    
        elif n % 5==0:
            print n, "Multiple of 5"
    
        elif n % 3==0:
            print n, "Multiple of 3"
    
        else:
            print n  "No multiples"
    

提交回复
热议问题