public class Solution {
public int addDigits(int num) {
if(num<10){
return num;
}
else{
return 1+((num-1)%9);
}
}
}
Simple C solution
int addDigits(int num) {
if(num==0)
return 0;
else if(num%9==0)
return 9;
else
return num%9;
}
Obviously,if the number provided is 0,the required answer is 0.And if the number provided is a multiple of 9,the required answer is 9,you can try that and the last case is if the number provided does not satisfy the above cases,then the required answer is (provided number%9).