C++ beginner - Sum of multiples less than 100

This is a discussion on C++ beginner - Sum of multiples less than 100 within the C++ Programming forums, part of the General Programming Boards category; Hey guys, this is my first post here. I am a beginner in C++ and have a question for a ...

  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
    22,269
    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.
    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

  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,340
    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
    Posts
    23,012
    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
    22,269
    Looks good. I note that you forgot to use the end variable in addMultiples though.
    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

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

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