algorithm - Sum of digits in C# -
what's fastest , easiest read implementation of calculating sum of digits?
i.e. given number: 17463 = 1 + 7 + 4 + 6 + 3 = 21
you arithmetically, without using string:
sum = 0; while (n != 0) { sum += n % 10; n /= 10; }
Comments
Post a Comment