Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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. (1000 not included)
- #include <iostream>
- using namespace std;
- bool multipleOf3(int x)
- {
- int factor_3 = x / 3;
- if (factor_3*3 == x) return true;
- else return false;
- // can also use if (x % 3 == 0) to find if x is a multiple of 3
- }
- bool multipleOf5(int y)
- {
- int factor_5 = y / 5;
- if (factor_5 * 5 == y) return true;
- else return false;
- }
- int main()
- {
- int i = 1;
- unsigned long int sum = 0;
- while (i < 1000)
- {
- if ((multipleOf3(i) == true) || (multipleOf5(i) == true)) sum += i;
- i++;
- }
- cout << sum << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement