# how to calculate the sum of all digit present in the array .

Page 1 of 1

## 6 Replies - 1335 Views - Last Post: 28 September 2012 - 12:05 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293297&amp;s=416c4722e1d77634d397c42c52bb080e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Usman Ali

Reputation: 0
• Posts: 2
• Joined: 26-September 12

# how to calculate the sum of all digit present in the array .

Posted 26 September 2012 - 11:11 AM

Problem:-
My task is how to calculate the sum of all digits present in the array. Each input must be a tree digits number?

This post has been edited by jon.kiparsky: 26 September 2012 - 11:14 AM
Reason for edit:: Removed failed formatting

Is This A Good Question/Topic? 0

## Replies To: how to calculate the sum of all digit present in the array .

### #2 jon.kiparsky

• Beginner

Reputation: 11067
• Posts: 18,903
• Joined: 19-March 11

## Re: how to calculate the sum of all digit present in the array .

Posted 26 September 2012 - 11:14 AM

Look at each digit. Add it to the previous one. Repeat.

### #3 NathanMullenax

Reputation: 103
• Posts: 218
• Joined: 23-September 12

## Re: how to calculate the sum of all digit present in the array .

Posted 26 September 2012 - 11:18 AM

This will probably be useful if the numbers are stored as integers:

To extract the least significant digit from n:
int d = n%10;

Number representing the remaining digits:
int r = n/10;

For example, if n was 567:
int d = 467%10; // 7
int r = 467/10; // 46

Hope this helps.

NathanMullenax, on 26 September 2012 - 11:17 AM, said:

This will probably be useful if the numbers are stored as integers:

To extract the least significant digit from n:
int d = n%10;

Number representing the remaining digits:
int r = n/10;

For example, if n was 467:
int d = 467%10; // 7
int r = 467/10; // 46

Hope this helps.

Oops, I mean 467

### #4 Usman Ali

Reputation: 0
• Posts: 2
• Joined: 26-September 12

## Re: how to calculate the sum of all digit present in the array .

Posted 26 September 2012 - 11:21 AM

that is the main problem how ?

kindly elaborate more clearly..???

### #5 NathanMullenax

Reputation: 103
• Posts: 218
• Joined: 23-September 12

## Re: how to calculate the sum of all digit present in the array .

Posted 26 September 2012 - 11:30 AM

Usman Ali, on 26 September 2012 - 11:21 AM, said:

that is the main problem how ?

kindly elaborate more clearly..???

It's more of a hint. The '%' operator means modulus or remainder. In order to sum this digits of a particular number you'll need to be able to extract each individual digit.

You're likely to get better responses if you post your code. You should probably start by looping over all elements in the array.

### #6 jon.kiparsky

• Beginner

Reputation: 11067
• Posts: 18,903
• Joined: 19-March 11

## Re: how to calculate the sum of all digit present in the array .

Posted 26 September 2012 - 11:34 AM

Easiest if you write a method that gets the sum of digits for one integer.

The signature of that method is

private int sumOfDigits(int num)
{
int total = 0;

// use what Nathan has showed you, in a loop

}

Then loop over the array and call this method on each integer in the array, summing the results.

### #7 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: how to calculate the sum of all digit present in the array .

Posted 28 September 2012 - 12:05 AM

You need to use a loop, preferably a 'for' loop, where you increment the sum by adding element to its previous element.This is a clue for which you need to write the code.

regards,
Raghav