int num = 123;
int sum = 0;
while(num > 0)
{
sum += number % 10;
num /= 10;
}
Console.WriteLine(sum); // output: 6
using System;
public class Program
{
public static int SumDigits(int inputInt)
{
string inputString = inputInt.ToString();
int sumDigits = 0;
for(int i = 0; i < inputString.Length; i++)
{
int currentDigit = int.Parse(inputString[i].ToString());
sumDigits += currentDigit;
}
return sumDigits;
}
public static void Main()
{
//test value -> output 30
Console.WriteLine(SumDigits(464646));
}
}
// C# program to compute
// sum of digits in number.
using System;
class GFG {
/* Function to get sum of digits */
static int getSum(int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
// Driver code
public static void Main()
{
int n = 687;
Console.Write(getSum(n));
}
}
sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
int result = 17463.ToString().Sum(c => Convert.ToInt32(c));