class TestJava { public static int sumOfDigits(int number){ if (number < 10) return -1; int sum = 0; while (number > 10){ sum += number number /= 10; } return sum + number; } public static void main(String[] args) { int number = 234567; int sumOfDigits = sumOfDigits(number); System.out.println("Number: " + number); System.out.println("Sum Of Digits: " + sumOfDigits); } }
Output:
Number: 234567 Sum Of Digits: 27