1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Simple Programming Question

  1. Nov 27, 2011 #1
    I'm teaching myself C++ and am stuck on this VERY simple problem. I'm not sure what's wrong with my code.

    The problem is from https://projecteuler.net/ if anyone is wondering.

    1. The problem statement, all variables and given/known data
    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.

    2. Relevant equations

    3. The attempt at a solution
    Code (Text):
    #include <iostream>
    using namespace std;

    int main()
        int num, add;

        num = 1;
        add = 0;
        while(num < 1001)
            if(num % 3 == 0 || num % 5 == 0)
                add = num + add;
            num = num + 1;
        return 0;
    The answer I get from this is 234168 but the site says this is wrong.
  2. jcsd
  3. Nov 27, 2011 #2
    Annnnd I figured out what I did wrong. The problem states below rather than up to 1000. It included 1000 in the answer so I just subtract that and be on my way.

    If there are any bad habits that are in my code, I would like to know just so I don't continue using it.

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook