sum of digits c# code example
Example 1: sum of digits in c#
int num = 123;
int sum = 0;
while(num > 0)
{
sum += number % 10;
num /= 10;
}
Console.WriteLine(sum); // output: 6
Example 2: sum the digits in c#
sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}