Say I have the number 123
Is there a computationally efficient way of adding 1+2+3 and getting the answer back
Ideally I'd avoid division or parsing to String as I feel this can be quite inefficient.
Say I have the number 123
Is there a computationally efficient way of adding 1+2+3 and getting the answer back
Ideally I'd avoid division or parsing to String as I feel this can be quite inefficient.
No need of parsing to string.
int sum=0;
while(n>0)
{
sum += n%10; // add the last digit
n/=10; // remove the last digit.
}