Question no. 1 :
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<iostream>
using namespace std;
int main(){
int sum3=0;
int sum5=0;
int sum15=0;
for (int i=1;i<=333;i++){
sum3= i + sum3;
}
for (int i=1;i<=199;i++){
sum5= i + sum5;
}
for (int i=1;i<=66;i++){
sum15= i + sum15;
}
int total = 3*sum3+5*sum5-15*sum15;
cout<<"Sum of multiples of 3 or 5 : "<<total;
return 0;
}
Output :
Sum of multiples of 3 or 5 : 233168
Top comments (0)