#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    I need help to sum numbers in c++


    I have to create a program within 10 hours to sum numbers in this form:
    for example: 43+44+46+49+53+...

    I'm new at programming and i couldnt do it.. but i need it for school. I had it for homework.

    Some help would be much appreciated..

    Comments on this post

    • ptr2void disagrees : Good luck with that.
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,749
    Rep Power
    9397
    Originally Posted by festi19
    I had it for homework.
    Operative word being "had".
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,074
    Rep Power
    1802
    The requirements are unclear. Are you saying that the user input is in that form, or are you merely showing us what calculation the program is to perform? If all the program does is sum, you don't need the + separator, you could use spaces or newline - that would be easier to process.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    The code that i was looking for was like this:

    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main()
    {
    double v=0;
    int i,c;
    i=44;
    c=2;

    do
    {
    v=v+i;
    i=i+c;
    c=c+1;

    }while (i<=121)


    cout<<v;
    return 0;
    }

    maybe i was unclear.. thanks for replies..
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,074
    Rep Power
    1802
    Slightly mor compact:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int v = 0 ;
        int c = 1 ;
    
        for( int i = 44; i <= 121; i += c )
        {
            v += i ;
            c++ ;
        }
    
        cout << v ;
     
        return 0;
    }
    Perhaps your question should have been:

    "Calculate the sum of the series of numbers 44 <=x <= 121, where each successive number x is n more then the previous one where n starts from 1 and increases by one on each iteration: e.g. 43+44+46+49+53+..."

IMN logo majestic logo threadwatch logo seochat tools logo