# Problem in finding the sum of integer digits

Page 1 of 1

## 3 Replies - 495 Views - Last Post: 25 November 2012 - 04:18 AMRate 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=301491&amp;s=32247c4d81d738c8b6f80858e497ab3a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 user12345

Reputation: 0
• Posts: 6
• Joined: 25-November 12

# Problem in finding the sum of integer digits

Posted 25 November 2012 - 02:16 AM

I have tried to get the sum of integer digits but i only get the sum of 12345 right. any other integer i enter, it gives the wrong sum. Please tell me where i am going wrong

my code is
```
#include <iostream>
#include <stdlib.h>
using namespace std;
void sumdigits(int );
void sumdigits(int )
{
int a;
int b[5];
int sum=0;
cout<<"Fill array with integer digits"<<endl;
cin>>b[5];
for ( a = 0 ; a < 6 ; a++)
{
//Enter the integer values one by one to store in array
sum = sum + b [5];
}
cout<<"The sum of the digits is: "<<sum<<endl;
}
void main ()
{
int integer;
cout<<"Enter integer: ";
cin >> integer;
sumdigits(integer);

}

```

This post has been edited by JackOfAllTrades: 25 November 2012 - 03:54 AM
Reason for edit:: Fixed code tags

Is This A Good Question/Topic? 0

## Replies To: Problem in finding the sum of integer digits

### #2 snoopy11

• Engineering ● Software

Reputation: 1017
• Posts: 3,092
• Joined: 20-March 10

## Re: Problem in finding the sum of integer digits

Posted 25 November 2012 - 02:38 AM

hi,

this line

sum = sum + b [5];
should be

```sum = sum + b [a];
```

Please use code tags [ code] [ \code]

Snoopy

### #3 AKMafia001

• </code.in.dream>

Reputation: 237
• Posts: 737
• Joined: 11-June 11

## Re: Problem in finding the sum of integer digits

Posted 25 November 2012 - 02:39 AM

Well! Lots of things to point out...
First, why you are passing an integer value to the sumdigits() which is never used in it...

Your array contains 5 elements, and you are counting 6, a should go till 4, so a < 5:
```for ( a = 0 ; a < 6 ; a++)

```

You are just adding some garbage value here, instead of the array index:
```sum = sum + b [5];
```

The final index of the array is 4, 0-4, in order to add all the elements use the array index, b[a]....

Hope this Helps!

PS: And one thing real serious, cin>>b[5];...
You are thinking that this read 5 inputs in the array, but it doesn't...

You are accessing a location that is out of the array elements, a location out of bounds...

Even if you do this a[4], you will only read in the 5th location of the array, and not the whole...

So, just use a loop from 0-4 and take input on each index...

This post has been edited by AKMafia001: 25 November 2012 - 02:55 AM

### #4 user12345

Reputation: 0
• Posts: 6
• Joined: 25-November 12

## Re: Problem in finding the sum of integer digits

Posted 25 November 2012 - 04:18 AM

Oh i got it now. Thanks a lot.