I know this isn't the way that "real" programmers would solve this problem, but this is the first thing I thought of. I know that my a, b, and c are correctly identified as 5, 4, and 6, and it prints out the correct sum of 15. But I still fail the last requirement. WHY?!
package com.codegym.task.task01.task0132;
/*
Sum of the digits of a three-digit number
*/
public class Solution {
public static void main(String[] args) {
System.out.println(sumDigitsInNumber(546));
}
public static int sumDigitsInNumber(int number) {
int i=0;
int a=0;
while (number-i> 0) {
i = i + 100;
a++;
}
a= a-1;
number= number-(a*100);
int j=0;
int b=0;
while (number-j>0) {
j = j + 10;
b++;
}
b=b-1;
int c= number-(b*10);
return a+b+c;
}
}