It's very simple, actually:
int singleDigitSum(int n) {
int sum = n % 9;
while (sum == 0) {
sum += 9;
}
return sum;
}
Explanation: for a given number N, this single digit sum is actually either the remainder of N divided by 9, or, if that remainder is 0, 9 itself. So loop here is used only to 'fast-forward' the result to 9 in that last case.