# sum of integers

• 06-03-2004
campermama
sum of integers
How do I write a function to take an integer and return the sum of integers from 1 to that number?

Thanks!!
• 06-03-2004
eth0
you should try and put something together first. We can then help you with anything your struggling with.
• 06-03-2004
campermama
ok I came up with this,but it want you to put in 10 numbers. I want to put in a number between 1 and 10, so if I put in the number 5 it should return 15 since 1 + 2+3+4+5=15. I know this code isn't right.

Code:

```int num; int sum = 0; int main() { cout << "Enter a number between 1 and 10: "<< endl; cin >> num; {  for (int num = 1; num <= 10; num ++)         sum += num;  }         cout << "The sum is " << sum ;                 return 0;                 }```
• 06-03-2004
MrWizard
Very close. Why don't you name your loop index something else? You named your global variable num that gets set to the users input. A common choice for loop index is i. Your stopping condition is "num <= 10". You don't always want it to go to 10 though. You want it to stop when you get to the number the user entered.
• 06-03-2004
eth0
As MrW said, its all about getting your algorithm correct first then building on that.

Yous syntax is pretty much correct (except you don't need the braces around your loop).

Just put an include file at the top so cin and cout can be recognised. You'll also need to declare standard namespace to make things easier at the moment.

Translated :
Code:

```#include <iostream> using namespace std;```
at the top of your code.

Now just work out your algorithm. You don't need to be a programmer to work it out, its simple maths.
• 06-03-2004
campermama
Ok I have this, I don't know how to make it stop at what the user enters.

Code:

``` #include <iostream> using namespace std; int num; int sum = 0; int main() { cout << "Enter a number between 1 and 10: "<< endl; cin >> num; {  for (int i = 1; num <= 10; i ++)         sum += i;  }         cout << "The sum is " << sum ;                 return 0;                 }```
• 06-03-2004
eth0

start the loop at 1
loop until the entered number reaches 10
sum = sum + 1

That will give you
0+1
1+1
2+1
3+1
4+1

you need to be using the sum to add with and looping until the sum is reached.
• 06-03-2004
campermama
So should it be something like this? I understand what you are saying, just having a hard time coding it.

Code:

``` #include <iostream> using namespace std; int num; int sum = 0; int main() { cout << "Enter a number between 1 and 10: "<< endl; cin >> num; {  for (int i = 1; sum = num; )         sum += num;  }         cout << "The sum is " << sum ;                 return 0;```
• 06-03-2004
eth0
Good luck :)

Code:

```#include <iostream> using namespace std; int main() {   int num;   int sum = 0;   cout << "Enter a number between 1 and 10: ";   cin >> num;   for (int i=1; i<=num; i++) {         sum += i;   }           cout << "The sum is " << sum << endl;           return 0; }```
• 06-03-2004
campermama
Oh my gosh!! Ok, I am tired! I totally missed that.

Thanks for the help!! I am reading and asking and trying very hard. I am learning from all of you that are helping me.

Thanks again!
• 06-04-2004
Sometimes it is hard to get right into the text editor and put your code right in. Try writing it out on paper without all the syntax. Sometimes it makes it easier to think. For example...

Code:

```while (i < 10)   {   print "blah"   i++;   }```
or something of that nature.
• 06-04-2004
CompiledMonkey
Good example for recursion.
• 06-05-2004
laserlight
You could also 'cheat' by applying the formula for this case.
i.e.
sum = (num*(num+1))/2

Code:

```#include <iostream> using namespace std; int main() {         int num;         cout<<"Enter a number between 1 and 10: ";         cin>>num;         int sum = (num * (num + 1)) / 2;         cout<<"The sum is:"<<sum<<endl;         return 0; }```
Of course, you arent actually checking if the number entered is between 1 and 10 (inclusive?)...
but I'll leave that part to you.