6 Replies - 801 Views - Last Post: 28 September 2012 - 12:05 AM Rate Topic: -----

#1 Usman Ali  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7891
  • View blog
  • Posts: 13,417
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • 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.

View PostNathanMullenax, 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 :)
Was This Post Helpful? 1
  • +
  • -

#4 Usman Ali  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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..???
Was This Post Helpful? 0
  • +
  • -

#5 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

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

Posted 26 September 2012 - 11:30 AM

View PostUsman 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.
Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7891
  • View blog
  • Posts: 13,417
  • 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

  return total;
}


Then loop over the array and call this method on each integer in the array, summing the results.
Was This Post Helpful? 1
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1