Hello everyone this is my first post on the daniweb community.
Currently I am home sick with a nasty cold so I was trying to solve a programming problem on a wonderfull sight called Project Euler.

The problem is as follows:
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.

Based on this problem I wrote this piece of code, which returns the value 266,333:

#include <iostream>

using namespace std;

int main()
{
    int x = 0;

    for(int i = 0; i < 334; i++)
    {
        x += 3 * i;
    }
    for(int i = 0; i < 200; i++)
    {
        x += 5 * i;
    }
    cout << x << endl;
    return 0;
}

However, this answer is wrong and I can not find a problem with my code any suggestions?

Thanks for the help and sorry if my code is not written that well, or sloppy I have just started learning programming for my school robotics team.

Recommended Answers

All 5 Replies

Some numbers are both multiple of 3 AND a multiple of 5. You count such numbers twice.

Ahh right... forget what I said

Thank you that makes a lot of sense know, plus it shouldn't be to hard to fix.

most basic code for this problem(not the best,can be done with pencil & paper )is
for(i=1;i<1000;i++){
if(i%3==0||i%5==0){count++;}}

many better ways than this

p.s. am currently running a program for project euler(will take days) so i can access the forum for the problem and see how the clever people did it.
great site but very addictive

edit

instead of count++
should be sum+=i

forgot what u asked
my bad

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.