I built 2 methods 1 get a number's digits, the 2nd sums all the digits of the number. It works , but for some reason when i type the number 11111 or higher it returns a wrong result.
For ex. the number 11111 returns 3 instead of 5
public class test_2 {
public static int getDigits(int x) {
int counter = 0;
while (x > 0) {
x /= 10;
counter++;
}
return counter;
}
public static int getNumber(int y) {
int New = 0;
for (int i = 0; i <= test_2.getDigits(y); i++) {
New += (y % 10);
y /= 10;
if (y < 10 && y > 0)
New += (y % 10);
}
return New;
}
}