Thread: sum of multiples of 3 or 5 below 1,000

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    24

    sum of multiples of 3 or 5 below 1,000

    I wrote a program which should compute the sum of all of the multiples of 3 or 5 below 1,000.
    Code:
    int main()
    {
    	int sum = 0;
    	int i;
    
    
    	for (i=0; i < 1000; i++)
    	{
    		if (i % 3 == 0)
    		sum += i;
    		
    
    		if (i % 5 == 0)
    		sum += i;
    	}
    
    	printf(" \n\n%i\n\n", sum);
    
    	return 0;
    }
    This program produces 266,333. According to the internet the correct answer is 233,168 which I verified in another program in ruby. Any thoughts as to why I am getting this incorrect result?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your if statements are not mutually exclusive, so you count numbers that are multiples of both 3 and 5 twice, e.g. 15, 30, ....

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are double counting. Try i = 15, for example.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Like this...

    Code:
    int main( void )
    {
       int sum = 0;
       int i;
    
       for (i=0; i < 1000; i++)
         {
            if (i % 3 == 0)
              sum += i;
            else if (i % 5 == 0)
              sum += i;
         }
       printf(" \n\n%i\n\n", sum);
       return 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Personally, I would write that part as:
    Code:
    if (i % 3 == 0 || i % 5 == 0)
    {
        sum += i;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    24
    Ahh, I see. Thank you all.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    My pleasure...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiples of 3 or 5
    By Matt Lord in forum C Programming
    Replies: 6
    Last Post: 09-05-2011, 05:16 PM
  2. Find the sum of all the multiples of 3 or 5 below 1000.
    By bdeepak23 in forum C Programming
    Replies: 21
    Last Post: 11-01-2009, 12:22 AM
  3. infinite loop?multiples of 2/
    By o0o in forum C++ Programming
    Replies: 10
    Last Post: 12-27-2003, 01:44 PM
  4. Rand() with multiples
    By Tommaso in forum C Programming
    Replies: 3
    Last Post: 10-21-2002, 08:11 PM

Tags for this Thread