Sum of the numbers between two integers

This is a discussion on Sum of the numbers between two integers within the C++ Programming forums, part of the General Programming Boards category; I need help here. I need a program that will calculate the sum of the numbers between two integers that ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    36

    Sum of the numbers between two integers

    I need help here. I need a program that will calculate the sum of the numbers between two integers that are assigned a value using cin. For example, say the two numbers were 25 and 47. Using a for loop, how do you calculate
    "25+26+27+28+29+....+47"

    Please help me. I am a noob at this.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You can change the beginning and ending values for the for loop, they don't always have to start at 0.

    I'd say make a program that takes in two numbers from the user and make sure it compiles and works. Then add the code to just output the numbers (without adding them) in the loop. Make sure that compiles and works. Then instead of outputting them, add them together. Make sure it compiles and works. Then you are done. If you get stuck along the way, post the code you've done so far and ask a specific question about what you are stuck with.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    36
    So far i have this

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    
    int start, end;
    
    cout<<"Enter one #: ";
    cin>>start;
    cout<<"The second #: ";
    cin>>end;
    
    for (int x=start; x<=end; x++)
    {
    
             This is the part I need. The math of the program.
    
    }   
    
    cout<<"The sum of the integers btwn these 2 numbers is "<<x;
    
    return 0;
    }

    I need the statements in the for loop.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Code:
    removed
    Last edited by neandrake; 12-07-2004 at 04:04 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That results in far too little, neandrake.

    Sridar, you need an additional variable to hold the sum.

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    
      int start, end;
    
      cout<<"Enter one #: ";
      cin>>start;
      cout<<"The second #: ";
      cin>>end;
    
      int sum = 0;
      for (int x=start; x<=end; x++)
      {
    
             Can you guess now?
    
      }   
    
      cout<<"The sum of the integers btwn these 2 numbers is "<<sum;
    
      return 0;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    36
    I still dont get it. Just Tell me what goes in the for loop.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    sum += x;

    And now please bang your head on the desk
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    >> That results in far too little, neandrake.

    It didn't feel right as I typed it, but it looks correct to me. What's the problem?

    [edit]
    shyte! Man, I must be high ;-)
    Yea, pay no attention to my code.

    You should create a variable outside of the for loop, set to zero. Inside the for loop, just add i to the new variable and it adds them up.
    [/edit]
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    36
    *Sridar bangs his head on the desk*

    !bang,bang,bang!

    Thank you so much Corned Bee

  10. #10
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by Sridar
    *Sridar bangs his head on the desk*

    !bang,bang,bang!

    Thank you so much Corned Bee

    //me follows
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I know this is irrelevant because this is a homework about for-loops, but I still have to point out:
    The sum of every integer between and including a and b is
    (a+b)(a-b+1)/2
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    wow, who agrees I should get negative reputation for a small mistake in the code? (removed)
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  13. #13
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by Sang-drax
    I know this is irrelevant because this is a homework about for-loops, but I still have to point out:
    The sum of every integer between and including a and b is
    (a+b)(a-b+1)/2
    Well (a-b+1) is the number of numbers between a and b assuming a is the larger. Multiply it by the average of both numbers. Yea, it seems like it should give you the right answer. However, you might get wrong answers since working with integers.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No. As the sum of all these numbers can only be an integer, the formula logically can only yield an integer, too, even if it were to be executed in floating point calculations.
    Or, in a less intuitive but more correct way:
    In order for the /2 to yield an integer, the left side must be even.
    (a+b)(a-b+1) -> even
    An expression of the form x*y is even if either or both of the terms are even, or reverse, it's uneven exactly when both terms are uneven.
    x is (a+b)
    y is (a-b+1)
    Since the evenness of (a+b) and (a-b) is the same, the +1 makes sure that, if (a+b) is even, (a-b+1) is uneven and vice versa. This means that x and y always have different evenness, that is, the above condition (both uneven) can never be fulfilled. (a+b)(a-b+1) is always even and the divison by two thus always yields an integer.
    No wrong answers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    ain
    ain is offline
    Registered User
    Join Date
    Feb 2011
    Posts
    7

    Question

    i know this will be a late reply but i need to ask what is the first number enter is greater than the first one?can this code proceed and give the right answer? because i have try before and it doesnt give the right answer. what should i do to fix it so that it can give the right answer?Thanx

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a sum equal to or in excess of 100
    By lyoncourt in forum C Programming
    Replies: 6
    Last Post: 10-07-2007, 05:43 PM
  2. ?? with trying to sum numbers
    By Babs21 in forum C Programming
    Replies: 1
    Last Post: 09-16-2007, 03:58 PM
  3. max/min in a list of numbers
    By shockalaca in forum C Programming
    Replies: 2
    Last Post: 05-23-2005, 02:11 AM
  4. Prime Numbers and Array...
    By Deux in forum C Programming
    Replies: 12
    Last Post: 12-20-2004, 03:12 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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