DEV Community

Sahaja
Sahaja

Posted on

Project Euler #1 - Multiples of 3 and 5

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;
}
Enter fullscreen mode Exit fullscreen mode
 Output :
 Sum of multiples of 3 or 5 : 233168 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)