Sum of numbers

This is a discussion on Sum of numbers within the C++ Programming forums, part of the General Programming Boards category; The purpose of this program is to display the sums of all of the numbers from one to the input ...

  1. #1
    Registered User
    Join Date
    Oct 2009

    Sum of numbers

    The purpose of this program is to display the sums of all of the numbers from one to the input number. Example if i enter 5 i want it to add the numbers 1+2+3+4+5 = 15 or if i enter 225 it will add 1+2+.....225

    Here is what i am working with

    #include <iostream.h>
    void main()
    int Number, Sum;
    Sum = 0;
    cout << "Enter a number: " << endl;
    cin >> Number;
    while (Number >= 0)
    Sum = Sum + Number;
    cout << "Current Total " << Sum << endl;
    cout << "Enter a number: " << endl;
    cin >> Number;
    cout << "The total is " << Sum << endl;
    It works fine when i go in order 1+2+3+.....10 but it does not add all i need.

    Any hints will help thanks in advance.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Instead of asking for a new number, you should be subtracting one from the number.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2003
    std::cout << (Number*(Number+1)/2) << std::endl;

    or, if you want to learn programming, consider two loops. I am going to write the outer loop for you, because this is a good habit to get into early while you are learning
    while(std::cin >> Number && Number > 0) {
        int Sum = 0;
        std::cout << Sum;
    This solves a lot of common bugs, first off realize that && is left to right, so the left hand side gets evaluated first, the right hand side is evaluated only when the left is true. The result of a stream input operation, is a stream. This is what lets you do cin >> a >> b; If you evaluate a stream in a boolian context it is true only if all the operations prior to this have been successful. If someone inputs 15, and then a letter your program loops forever, when done this way it exits.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Not trying to suggest that you don't sum up those numbers, but the formula:
    y = x*(x+1)/2 gives the sum you are calculating.
    You can at least use that to check that your answers are correct.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. Help with programming assignment, please!
    By xMEGANx in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2008, 04:29 PM
  3. Help With Stacks
    By penance in forum C Programming
    Replies: 7
    Last Post: 10-09-2005, 02:47 PM
  4. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  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