mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 22:14:08 +08:00
Change project structure to a Maven Java project + Refactor (#2816)
This commit is contained in:

committed by
GitHub

parent
8e533d2617
commit
9fb3364ccc
60
src/main/java/com/thealgorithms/maths/SumOfDigits.java
Normal file
60
src/main/java/com/thealgorithms/maths/SumOfDigits.java
Normal file
@ -0,0 +1,60 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
public class SumOfDigits {
|
||||
|
||||
public static void main(String[] args) {
|
||||
assert sumOfDigits(-123) == 6 && sumOfDigitsRecursion(-123) == 6 && sumOfDigitsFast(-123) == 6;
|
||||
|
||||
assert sumOfDigits(0) == 0 && sumOfDigitsRecursion(0) == 0 && sumOfDigitsFast(0) == 0;
|
||||
|
||||
assert sumOfDigits(12345) == 15
|
||||
&& sumOfDigitsRecursion(12345) == 15
|
||||
&& sumOfDigitsFast(12345) == 15;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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';
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user