Q:-If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
#include<stdio.h>
int main(){
int a,sum=0;
//going through all numbers from 1-1000
for (a=1;a<1000;a++){
if (a%3==0 || a%5==0){//check whether each number is divisible by 3 or 5
sum +=a;} //if so add that number to sum
}
printf("sum is : %d", sum);
getchar();
}
Solution using Python(3):
sum=0
for i in range (1000):
if i%3==0 or i%5==0:
sum+=i
print(sum)
Solution using pen and paper:
using the formula Sn=n*(a+l)/2 use for both numbers divisible by 3 and 5 seperatly and add the sum.