Like Tree1Likes
  • 1 Post By laserlight

sum of multiples of 3 or 5 below 1,000

This is a discussion on sum of multiples of 3 or 5 below 1,000 within the C Programming forums, part of the General Programming Boards category; I wrote a program which should compute the sum of all of the 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,325
    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
    20,981
    You are double counting. Try i = 15, for example.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,981
    Personally, I would write that part as:
    Code:
    if (i % 3 == 0 || i % 5 == 0)
    {
        sum += i;
    }
    deadrabbit likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21