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.
I've solved the problem and it works but if the input is a large number, instead of no breaklines in every string printed, there is. Yes, it's ok to ignore it but I dont want to let this be one of my biggest problem which I can't figure out in the future. So anyone knows why?
#include <iostream>
usingnamespace std;
int main()
{
unsignedshortint tryAgain;
double sum = 0;
unsignedlongint userInput;
do
{
do
{
cout << "Enter the number: ";
cin >> userInput;
}while(userInput < 1);
for(int i = 1; i < userInput; i++)
{
if(i%3 == 0)
{
sum += i;
cout << i << " is multiple of 3. It's now added to variable sum which is now " << sum << endl;
};
if(i%5 == 0)
{
sum += i;
cout << i << " is multiple of 5. It's now added to variable sum which is now " << sum << endl;
};
};
cout << "The sum of its multiple is: " << sum << endl;
do
{
cout << "Wanna try again?" << endl;
cout << "1 - Yes" << endl << "2 - No" << endl;
cin >> tryAgain;
}while(tryAgain != 1 && tryAgain != 2);
if(tryAgain == 1) sum = 0;
}while(tryAgain == 1);
};
What do you mean? What makes you think something's wrong? Does it stop breathing? Does it explode? Does it install a virus? You're not giving us much to go on here.
Also, you have a lot of semicolons after closing braces, like on lines 23, 28, 29, and 39 - what are those for? They do not do what you expect them to, and you're not supposed to have them.
oh by the way, if you noticed that the problem is to know what is the multiple of 3 or 5 below 1000 and the code that i posted above, it's just a version of mine which you can find out the multiples of the number the user entered, not just 1000
If you're running the program in the console, note that when the text is long and reaches the edge of the screen, it inserts a line break for you (you have no choice). Then when your program puts its own line break, it looks like it put two - don't worry, this is just the console, your program is fine.
Alrighty. I thought I remembered someone directing you to the Project Euler page previously and this looked like an attempt to solve the first problem. For the purposes of that problem, doing so is incorrect.