# Sum up of numbers

This is a discussion on Sum up of numbers within the C++ Programming forums, part of the General Programming Boards category; Hi I would like to know how I can write a program that gives me the sum of 1 to ...

1. ## Sum up of numbers

Hi I would like to know how I can write a program that gives me the sum of 1 to n numbers. Example:

Code:
```#include <iostream>
using namespace std;

int main ()
{
int i =0;

//Here is a loop that, for example, gives me numbers from 0 to 4
//My problem is, I want to add these numbers
//I get 01234, now all I want is add them together
//I was thinking of maybe using an array to save these numbers, then add them
//one by one but I am a beginner and have no idea how to do it

for (i; i<5; i++)
{
cout<<i
}
return 0;
}```

2. Rather than saving them, why not just add them to some other variable, named say sumOfNumbers ?

3. I didn't understand properly what you meant by add them to another variable.

4. What Salem means is that instead of asking 10 numbers and remembering them, then summing them, why not ask for numbers continuously and add them as you go?
For example, instead of

User inputs 1
Remember 1
User inputs 2
Remember 2
...
Sum all inputted numbers

you do

User inputs 1
Total sum = 1
User inputs 2
Total sum = previous sum (1) + 2 = 3
User inputs 3
Total sum = previous sum (3) + 3 = 6
...

5. I wasn't asking for user inputs. I just want to know how I can sum up the individual numbers in a for loop. As in the simple example, I have numbers from 0 to 4 produced by the for-loop. I want to be able to sum these numbers up, that is, 0+1+2+3+4 = 10. Problem is, I can't get to individually access the numbers so that I can sum them up.

6. You HAVE the numbers already, they're in i, your for loop variable.

You managed to print them, why can't you add them?

7. Yeah that is the problem I guess. I get to print them. They are there but how can I get to sum them up? That is where I am stuck. I guess I need to sum them up recursively or something like that but I am stuck here.

8. Initialise the total to zero, then add each of those numbers in turn to the total.

9. Yeah got it finally, thanks all. I must be so thick. This is how I did it:

Code:
```#include <iostream>
using namespace std;

int main ()
{
int i =0, sum =0;
for (i; i<5; i++)
{
sum +=i;
}
cout<<sum;
return 0;
}```

10. Good, though this is how I would have written it:
Code:
```#include <iostream>
using namespace std;

int main()
{
int sum = 0;
for (int i = 0; i < 5; ++i)
{
sum += i;
}
cout << sum << endl;
return 0;
}```
You could also have implemented a formula to compute the sum for you.

11. Thanks laserlight, why would you write it that way? I mean I am new so I am just curious and want to know why would you do it that way. Any explanation, tips etc. are appreciated.

Ooh and I can see you have ++i instead of the i++ that I wrote, what is the difference?

12. Say you wrote h = i++; somewhere. The value of h is actually the previous value of i before you added. In h = ++i; h is actually the same as i. There are times where you will use the value of i in this step, but a for loop is not really one of those times. You could safely use either.

13. Originally Posted by Dontgiveup
Thanks laserlight, why would you write it that way?
• No more unused variable named x.
• i is only used within the loop, so it is moved to be within the loop.
• It does not matter here, but later you will find that conventionally, ++i is no slower than, but could be faster than i++.
• Consistent indentation and more common style (e.g., spacing between operators).
• Nice to write a newline at the very end, so I chose to do that with std::endl.

14. Originally Posted by laserlight
• No more unused variable named x.
• i is only used within the loop, so it is moved to be within the loop.
• It does not matter here, but later you will find that conventionally, ++i is no slower than, but could be faster than i++.
• Consistent indentation and more common style (e.g., spacing between operators).
• Nice to write a newline at the very end, so I chose to do that with std::endl.
Thanks so much. Will keep that in mind next time.

Originally Posted by whiteflags
Say you wrote h = i++; somewhere. The value of h is actually the previous value of i before you added. In h = ++i; h is actually the same as i. There are times where you will use the value of i in this step, but a for loop is not really one of those times. You could safely use either.
Thanks whiteflags!

Popular pages Recent additions