Skip navigation

Here’s my solution (pretty simple):

#include  <iostream>

int main (int argc, char * const argv[]) {
    int answer = 0;
    int value = 0;
    int original_value;
    std::cout << "Provide a value for which you would like to get the multiples of 3 or 5 below of: ";
    std::cout >> value;
    original_value = value;
    while(--value > 0){
      if(!(value % 3) || !(value % 5)){
        answer += value;
      }
    }
    std::printf("Value: %d Answer: %d\n", original_value, answer);
    return 0;
}

Pretty simple, and brute force. Got the answer in less than a second.

Advertisements

3 Comments

  1. You code has a few flaws.

    A) std::cout >> value;
    Should be) std::cin >> value;

    B) std::printf(“Value: %d Answer: %d\n”, original_value, answer);
    Should be) std::cout << original value << std::endl;
    std::cout << answer << endl;

    Always compile before you post.

  2. Sorry dude, my code compiles and it got the right answer.

  3. Oh wait you are right. Thanks.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: