# Sum of Numbers

Page 1 of 1

## 7 Replies - 3675 Views - Last Post: 24 October 2010 - 02:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=196558&amp;s=d2d009d2b332b9bf6abaa6c48a9589b3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cyber_gurl2010

• New D.I.C Head

Reputation: 0
• Posts: 25
• Joined: 19-October 10

# Sum of Numbers

Posted 24 October 2010 - 01:40 PM

Hello Everyone, I have been working on this program and here is my code and If anyone can give me a hint or some idea of how i can improve it.
So far, what i'm trying to do is find the sum of the integer i entered. so for example, i enter 5 the result will show 15. Actually, that is my problem because instead of the result showing 15 it shows 5 as the result. I know its adding 1 to each value so it shows 12345. what i want is 1+2+3+4, but my math formula is wrong. To make this work i have another formula to try and that is n= number. (n(n+1)/2. If i use this formula it doesn't work.

```// Sum of Numbers
// This ogram uses a loop to get the sum of all integers
// from 1 up to the number entered.

#include <iostream>
using namespace std;
int main()
{
int number;
int num = 1;
int sum = 0;
cout << “Enter a Positive Integer: “ << endl;
cin  >> number;

while(number <1)
{
cout << “ The number must be a postive integer: “ << endl;
cin  >> “ number;
}

[b]for ( sum = 1; sum <= number; sum++)
{ sum += number[/b]
}

cout << “The sum of the number is: “ << endl;

return 0;
}

```

This post has been edited by Martyr2: 24 October 2010 - 01:49 PM
Reason for edit:: Please use code tags in the future, thanks!

Is This A Good Question/Topic? 0

## Replies To: Sum of Numbers

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3177
• Posts: 11,259
• Joined: 08-August 08

## Re: Sum of Numbers

Posted 24 October 2010 - 01:52 PM

If the user enters 0, what will happen in your while loop?

### #3 eker676

• Software Engineer

Reputation: 378
• Posts: 1,833
• Joined: 18-April 09

## Re: Sum of Numbers

Posted 24 October 2010 - 01:53 PM

Look at your logic. You are resetting the sum variable every pass in the for loop.

Use a counter variable like 'i' and add it to sum.

Example:
```int sum = 0;

for(int i = 1; i <= number; i++)
{
sum += i;
}

cout << sum << endl;

```

### #4 Martyr2

• Programming Theoretician

Reputation: 4625
• Posts: 12,677
• Joined: 18-April 07

## Re: Sum of Numbers

Posted 24 October 2010 - 01:55 PM

You can also just do your formula correctly...

```// Sum of Numbers
// This ogram uses a loop to get the sum of all integers
// from 1 up to the number entered.

#include <iostream>
using namespace std;
int main()
{
int number;
int num = 1;
int sum = 0;
cout << "Enter a Positive Integer: " << endl;
cin  >> number;

while(number <1)
{
cout << " The number must be a postive integer: " << endl;
}

// Notice the formula in this line
cout << "The sum of the number is: " << (number * (number + 1)) / 2 << endl;

return 0;
}

```

Pretty straight forward to me.

Reputation:

## Re: Sum of Numbers

Posted 24 October 2010 - 02:00 PM

CTphpnwb, on 24 October 2010 - 12:52 PM, said:

If the user enters 0, what will happen in your while loop?

It is suppose to show invalid input . Guess I have to add that too.

### #6 cyber_gurl2010

• New D.I.C Head

Reputation: 0
• Posts: 25
• Joined: 19-October 10

## Re: Sum of Numbers

Posted 24 October 2010 - 02:05 PM

cyber_gurl2010, on 24 October 2010 - 12:40 PM, said:

Hello Everyone, I have been working on this program and here is my code and If anyone can give me a hint or some idea of how i can improve it.
So far, what i'm trying to do is find the sum of the integer i entered. so for example, i enter 5 the result will show 15. Actually, that is my problem because instead of the result showing 15 it shows 5 as the result. I know its adding 1 to each value so it shows 12345. what i want is 1+2+3+4, but my math formula is wrong. To make this work i have another formula to try and that is n= number. (n(n+1)/2. If i use this formula it doesn't work.

```// Sum of Numbers
// This ogram uses a loop to get the sum of all integers
// from 1 up to the number entered.

#include <iostream>
using namespace std;
int main()
{
int number;
int num = 1;
int sum = 0;
cout << “Enter a Positive Integer: “ << endl;
cin  >> number;

while(number <1)
{
cout << “ The number must be a postive integer: “ << endl;
cin  >> “ number;
}

[b]for ( sum = 1; sum <= number; sum++)
{ sum += number[/b]
}

cout << “The sum of the number is: “ << endl;

return 0;
}

```

### #7 CTphpnwb

• D.I.C Lover

Reputation: 3177
• Posts: 11,259
• Joined: 08-August 08

## Re: Sum of Numbers

Posted 24 October 2010 - 02:10 PM

cybergurl_2010, on 24 October 2010 - 04:00 PM, said:

CTphpnwb, on 24 October 2010 - 12:52 PM, said:

If the user enters 0, what will happen in your while loop?

It is suppose to show invalid input . Guess I have to add that too.

Sorry, I misread it as two couts. Your while loop will work.

### #8 cyber_gurl2010

• New D.I.C Head

Reputation: 0
• Posts: 25
• Joined: 19-October 10

## Re: Sum of Numbers

Posted 24 October 2010 - 02:11 PM

Thanks Everyone, for your ideas and my program is now working.