1. ## sum

Ok I know one way of using the formula n*(n+1)/2) and have used it to calculate the sum already but I just wanted to know how I can access many numbers in a loop and add them together. Here is what I did so far.

Code:
```#include <iostream>
using namespace std;
int main (){
int num, num1, sum;
cin>>num;
cout <<"Please enter the number to which you want to count\n";
cin>>num1;
for (; num<=num1; num++)
cout<<num<< ","; //I do get the numbers but from here
//I want to know what I can do so that I can add each
// of these numbers together?
do (sum=num+num);//I want to add them like 0+1+2+...
//tried using loops but couldn't get it
while (num<=sum);
cout<<sum;//but this does not work so any help is appreciated
return 0;
}```

2. I'm a little unsure on what you are trying to do. Do you want the user to enter an initial number, then have the user enter that many numbers and add them together. So if the user enters "3", the program will prompt the user to enter 3 numbers which get added together?

3. The user enters a number first. Then s/he enters a second number (that should be greater than the first one) and so the program counts from the number entered first to the second number. So the user enters for example enters 3 first, and then 10 and the program will count to 10 i.e. it will display 3,4,5,6,7,8,9,10. Now what I want is to get the sum, average and standard deviation of these numbers such that the program sums up 3+4+5+6+7+8+9+10 to get the sum and then divides by8 to get the average and calculate for the deviation and so on.

4. Well let's start with the sum. What you basically have so far is:
Code:
```for (; num<=num1; num++)
{
cout << num << endl;
}```
Now for each of those numbers, you need to add them up. So a quick change to your code:
Code:
```int total = 0;
for (; num<=num1; num++)
{
cout << num << endl;
total = ... // what goes here?
}```
From this, can you figure out what to set total equal to?

5. Originally Posted by bithub
Well let's start with the sum. What you basically have so far is:
Code:
```for (; num<=num1; num++)
{
cout << num << endl;
}```
Now for each of those numbers, you need to add them up. So a quick change to your code:
Code:
```int total = 0;
for (; num<=num1; num++)
{
cout << num << endl;
total = ... // what goes here?
}```
From this, can you figure out what to set total equal to?
total = total + num?

6. bingo.

7. Thanks!

8. But is there a way in which I can access the digits individually? I mean if I want to calculate the standard deviation, then I have to access each digit alone and subtract from average etc.

9. The for loop is how you access them individually. You have already done this in your code up above.

10. The other things I don't understand is why is it that it looks like there is an extra number when calculating average? For example you have numbers from 1 to 9 and you get the right total which is 45 but instead of getting an average of 5, you get 4. Why is that?

11. Originally Posted by Dontgiveup
For example you have numbers from 1 to 9 and you get the right total which is 45 but instead of getting an average of 5, you get 4. Why is that?
Sounds like integer division, so if you are required to use integers only, 4 would be correct.

12. What does your code look like? I'm guessing that you are dividing 45 by 10 instead of 9.

13. Originally Posted by bithub
I'm guessing that you are dividing 45 by 10 instead of 9.
Oh yes, sum from 1 to 9 rather than 0 to 9, so yeah, an off by one error is more likely (or should I say "as likely"?).

14. Originally Posted by laserlight
Oh yes, sum from 1 to 9 rather than 0 to 9, so yeah, an off by one error is more likely (or should I say "as likely"?).
Here is my code.
Code:
```#include <iostream>
using namespace std;
int main (){
int num, num1, average;

cin>>num;
cout <<"Please enter the number to whcih you want to count\n";
cin>>num1;
int sum = 0;
for (; num<=num1; num++)
{
cout << num << endl;
sum = sum +num; // this is fine and all is OK
}
average=(sum/(num+1));//but here is the problem. Try with 1 and 12 and you get
// the sum of all numbers from 1 to 12 i.e. 78 but you get the average as 5!
cout<<sum<<average;
return 0;
}```

15. You probably want the average as a floating point variable.
Code:
```#include <iostream>
using namespace std;
int main (){
int num, num1;
float average;

cin>>num;
cout <<"Please enter the number to whcih you want to count\n";
cin>>num1;
int sum = 0;
for (; num<=num1; num++)
{
cout << num << endl;
sum = sum +num; // this is fine and all is OK
}
average=(static_cast<float>(sum)/(num+1));//but here is the problem. Try with 1 and 12 and you get
// the sum of all numbers from 1 to 12 i.e. 78 but you get the average as 5!
cout<<sum<<average;
return 0;
}```