Advertisement
hb20007

Euler1

Nov 7th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. //If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
  2. //The sum of these multiples is 23.
  3. //Find the sum of all the multiples of 3 or 5 below 1000. (1000 not included)
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. bool multipleOf3(int x)
  9. {
  10.     int factor_3 = x / 3;
  11.     if (factor_3*3 == x) return true;
  12.     else return false;
  13.         // can also use if (x % 3 == 0) to find if x is a multiple of 3
  14. }
  15.  
  16. bool multipleOf5(int y)
  17. {
  18.     int factor_5 = y / 5;
  19.     if (factor_5 * 5 == y) return true;
  20.     else return false;
  21. }
  22.  
  23. int main()
  24. {
  25.     int i = 1;
  26.     unsigned long int sum = 0;
  27.     while (i < 1000)
  28.     {
  29.         if ((multipleOf3(i) == true) || (multipleOf5(i) == true)) sum += i;
  30.         i++;
  31.     }
  32.     cout << sum << endl;
  33.     system("pause");
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement