Thread: Find the sum of all the multiples of 3 or 5 below 1000.

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    12

    Find the sum of all the multiples of 3 or 5 below 1000.

    NEVERMIND solved it. Dont know how to take this down.

    if i put in 10, i get a return of 24 instead of 23, can someone tell me where im going wrong.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main ()
    {
        int c;
        int n;
        int x;
        int y, z;
    
    
        printf("");
        scanf("%d", &n);
    
    
        for (c=0; c<n; c++)
        {
            if(c%3 ==0 || c%5==0)
            {
                x += c;
                printf("%d \n",x);
    
            }
    
    
        }
    
    
    }
    Last edited by Tokalosh; 07-08-2017 at 02:47 PM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    What did you do to correct the original code?

    There were several issues with the code. Please repost your corrected code. This helps others learning from your code.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    12

    fixed

    Quote Originally Posted by rstanley View Post
    What did you do to correct the original code?

    There were several issues with the code. Please repost your corrected code. This helps others learning from your code.
    it was a simple loop that had to be added.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /* summing the first 1000 numbers of multiples 3 & 5 */
    
    
    int main ()
    {
        int c;
        int n;
        int x;
        int y, z;
    
    
        printf("");
        scanf("%d", &n);
    
    
        for (c=0; c<n; c++)
        {
            if(c%3 ==0 || c%5==0)
            {
    
    
                printf("%d \n",c);
    
    
            }  else {
                    x+=c;
                }
    
    
        }
    printf("%d", x);
    
    
    return 0;
    }

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Compare the following code you your latest version, and see my comments in the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /* summing the first 1000 numbers of multiples 3 & 5 */
    
    int main (void)
    {
       //Initialize all local variables
       int c = 0;
       int n = 0;
       int x = 0;
    
       // y & z are not used
       // int y, z;
    
       // Prompt the user what to enter
       printf("Please enter the upper limit\n");
    
       scanf("%d", &n);
       // Check here that a valid value was entered.
    
       for (c=0; c<n; c++)
       {
          if(c%3 ==0 || c%5==0)
          {
             printf("%d \n",c);
          }
          else
          {
             x+=c;
          }
       }
    
       // Print a newline at the  end
       printf("%d\n", x);
    
       return 0;
    }
    Turn on and turn up your warning level!
    From my compile of your latest code:
    Code:
    sum.c: In function ‘main’:
    sum.c:14:11: warning: zero-length gnu_printf format string [-Wformat-zero-length]
        printf("");
               ^~
    sum.c:11:11: warning: unused variable ‘z’ [-Wunused-variable]
        int y, z;
               ^
    sum.c:11:8: warning: unused variable ‘y’ [-Wunused-variable]
        int y, z;
            ^
    Please indent your code consistently, and remove unneeded blank lines. Choose one style and be consistent.

    More prompts and explanation of the program purpose, and output are needed.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Alternate implementation:
    Code:
    #include <stdio.h>
    
    // Sum of values [1,n] that are divisible by d.
    #define SUM_OF_DIVS(n,d) (((n)/(d))*(((n)/(d))+1)/2*(d))
    
    int main()  {
        int n;
        printf("n: ");
        scanf("%d", &n):
        int x = SUM_OF_DIVS(n,3) + SUM_OF_DIVS(n,5) - SUM_OF_DIVS(n,15);
        printf("%d\n", x);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sum of multiples of 3 or 5 below 1,000
    By deadrabbit in forum C Programming
    Replies: 6
    Last Post: 09-22-2011, 12:56 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. find the largest 1000 values
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-08-2007, 05:12 AM
  4. Replies: 8
    Last Post: 09-04-2006, 07:54 PM
  5. Find integer 1- 1000 w/ most divisors w/o remainder
    By AlexDeToi in forum C++ Programming
    Replies: 3
    Last Post: 03-24-2002, 08:45 PM

Tags for this Thread