Following is a way to sum the digits into a single digit.
e.g. let we have to sum the digits of number 234578.
2 + 3 + 4 + 5 + 7 + 8 = 29; sum it again until a single digit.
2 + 9 = 11; sum it again until a single digit.
1 + 1 = 2 ; it is final sum of digits.
so sum of digits of number 234578 is 2.
Here is the code for sum of digits:
For any given positive integers n;
if (n != 0) {
if (n % 9 == 0) {
return 9;
}
return n % 9;
}
return 0;
Piece of code will always return sum of digits.