DEV Community

Sahaja
Sahaja

Posted on

1 1

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)

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay