0

Ok so I have been trying to do project euler problem 1 which finding the sum of all the multiples of 3 and 5 that are less than a thousand. i dont know why my program is not working. here's my program -

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int multiplier3 = 1; // multiplier of 3
    int multiplier5 = 1;//multiplier of 5
    int mult3 = 0; // multiple of 3
    int ans3 = 0; // sum of multiples of 3
    int mult5 = 0;//multiple of 5
    int ans5 = 0;// sum of multiples of 5
    int answer = 0;// sum of the sum of multiples of 3 and the multiples of 5
    
    while (ans3 < 1000){
          mult3 = multiplier3 * 3;
          ans3 = ans3 + mult3;
          multiplier3 = multiplier3 + 1;
          mult3 = 0;
          }
          
    while (ans5 < 1000){
          mult5 = multiplier5 * 5;
          ans3 = ans5 + mult5;
          multiplier5 = multiplier5 + 1;
          mult5 = 0;
          }     
          
   answer = ans3 + ans5;
   
   cout << answer << endl;        
          
          
          
    system("PAUSE");
    return EXIT_SUCCESS;
}
2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by firstPerson
0

The key idea is to use the modulo operation.

For example if(num % 2 == 0){...} means that num is evenly divided by 2, thus checks if num is even. Use that idea. Your algorithm is more complicated than it needs to be.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.