Thread: C++ beginner - Sum of multiples less than 100

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    5

    C++ beginner - Sum of multiples less than 100

    Hey guys, this is my first post here. I am a beginner in C++ and have a question for a homework assignment. I have read my book over and over and can't seem to figure out how to code this properly.

    What I want it to do is have the user input a number: ex. 5 and then have the code give the sum for all the multiples of 5 less than 100, so it should be equal to 950 in this example.

    Anyway, I have got so far to get it to list the numbers, but I need the sum of those numbers without all the text displayed every time.

    I have chosen to use the FOR, but I am wondering if I should implement WHILE loop in my coding somewhere. I just don't know how to go about it for this problem.

    Again, this is my first post (and definitely not my last!) I hope I have read the rules properly. It is my first programming class in school, and I am seeing a tutor tomorrow and the next day, I would just like to have a better grasp of this before going in tomorrow.

    Code:
    #include <iostream>
    using namespace std;
    
    int addMultiples()
    {
       // Start
       cout << "What multiples are we adding? ";
       int start;
       cin >> start;
    
       int increment = start;
    
       for (int count = start; count <= 100; count += increment)
       cout << "The sum of multiples of " << start
            << " less than 100 are: " << count << endl;
    
       return 0;
    }
    
    
    
    
    int main()
    {
       addMultiples();
       return 0;
    }
    I want the output like this:

    What multiples are we adding? 5
    The sum of the multiples of 5 less than 100 are: 950

    Thanks for your help and explanations!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A for loop is fine. At the moment, the problem is that you are not adding up the multiples of 5 (or whatever it is).

    I suggest that you change your addMultiples function to take start as an argument, and to return the result. The input/output should all be done in the main function instead.
    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

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    5
    Quote Originally Posted by laserlight View Post
    A for loop is fine. At the moment, the problem is that you are not adding up the multiples of 5 (or whatever it is).

    I suggest that you change your addMultiples function to take start as an argument, and to return the result. The input/output should all be done in the main function instead.
    Thanks I will try to change the function to getMultiple, then have a function run the FOR loop. Any advice on how to get the sum from a loop?

    Thanks again, I feel I am on the right track!

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Bronzy View Post
    Any advice on how to get the sum from a loop?
    You need another variable, such as "sum", that is initialized to zero, and you need to add count to that sum for each iteration of the loop.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Bronzy View Post
    Any advice on how to get the sum from a loop?
    Tried making a flowchart?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    5
    Thanks for the help guys, I was able to have main call a few separate functions, I was missing the curly braces for the loop to get the Sum.

    Cheers!


    Code:
    #include <iostream>
    using namespace std;
    
    
    /**********************************************************************
     * getMultiple prompts the user for the multiple they want.
     ************************************************************************/
    int getMultiple()
    {
       cout << "What multiples are we adding? ";
       int number;
       cin >> number;
       return number;
    }
    
    
    /**********************************************************************
     * addMultiples initiates a loop for the multiple and sums the data.
     ************************************************************************/
    int addMultiples(int multiple)
    {
       int sum = 0;
       int end = 100;
    
    
       for (int count = multiple; count < 100; count += multiple)
       {
          sum += count;
       }
    
    
       return sum;
    }
    
    
    /**********************************************************************
     * Display outputs the sum of the multiples to the user.
     ************************************************************************/
    void display(int multiple, int sum)
    {
       cout << "The sum of multiples of "
            << multiple
            << " less than 100 are: "
            << sum
            << endl;
    
    
       return;
    }
    
    
    
    /**********************************************************************
     * Main calls the other functions in the program
     ************************************************************************/
    int main()
    {
       int multiple = getMultiple();
       int sum = addMultiples(multiple);
    
    
       display(multiple, sum);
    
    
       return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Looks good. I note that you forgot to use the end variable in addMultiples though.
    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

  8. #8
    Registered User
    Join Date
    May 2013
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Looks good. I note that you forgot to use the end variable in addMultiples though.
    Aha, you are right! I'll change that too

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. Multiples of 3 or 5
    By Matt Lord in forum C Programming
    Replies: 6
    Last Post: 09-05-2011, 05:16 PM
  3. 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
  4. Rand() with multiples
    By Tommaso in forum C Programming
    Replies: 3
    Last Post: 10-21-2002, 08:11 PM