2020-08-16 23:04:34 +08:00
|
|
|
package Maths;
|
|
|
|
|
|
|
|
public class SumOfDigits {
|
2020-10-24 18:23:28 +08:00
|
|
|
public static void main(String[] args) {
|
|
|
|
assert sumOfDigits(-123) == 6 && sumOfDigitsRecursion(-123) == 6 && sumOfDigitsFast(-123) == 6;
|
2020-08-16 23:04:34 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
assert sumOfDigits(0) == 0 && sumOfDigitsRecursion(0) == 0 && sumOfDigitsFast(0) == 0;
|
2020-08-16 23:04:34 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
assert sumOfDigits(12345) == 15
|
|
|
|
&& sumOfDigitsRecursion(12345) == 15
|
|
|
|
&& sumOfDigitsFast(12345) == 15;
|
|
|
|
}
|
2020-08-16 23:04:34 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Calculate the sum of digits of a number
|
|
|
|
*
|
|
|
|
* @param number the number contains digits
|
|
|
|
* @return sum of digits of given {@code number}
|
|
|
|
*/
|
|
|
|
public static int sumOfDigits(int number) {
|
|
|
|
number = number < 0 ? -number : number; /* calculate abs value */
|
|
|
|
int sum = 0;
|
|
|
|
while (number != 0) {
|
|
|
|
sum += number % 10;
|
|
|
|
number /= 10;
|
2020-08-16 23:04:34 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
return sum;
|
|
|
|
}
|
2020-08-16 23:04:34 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Calculate the sum of digits of a number using recursion
|
|
|
|
*
|
|
|
|
* @param number the number contains digits
|
|
|
|
* @return sum of digits of given {@code number}
|
|
|
|
*/
|
|
|
|
public static int sumOfDigitsRecursion(int number) {
|
|
|
|
number = number < 0 ? -number : number; /* calculate abs value */
|
|
|
|
return number < 10 ? number : number % 10 + sumOfDigitsRecursion(number / 10);
|
|
|
|
}
|
2020-08-16 23:04:34 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Calculate the sum of digits of a number using char array
|
|
|
|
*
|
|
|
|
* @param number the number contains digits
|
|
|
|
* @return sum of digits of given {@code number}
|
|
|
|
*/
|
|
|
|
public static int sumOfDigitsFast(int number) {
|
|
|
|
number = number < 0 ? -number : number; /* calculate abs value */
|
|
|
|
char[] digits = (number + "").toCharArray();
|
|
|
|
int sum = 0;
|
|
|
|
for (int i = 0; i < digits.length; ++i) {
|
|
|
|
sum += digits[i] - '0';
|
2020-08-16 23:04:34 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
return sum;
|
|
|
|
}
|
2020-08-16 23:04:34 +08:00
|
|
|
}
|