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;
}

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.