Finding sum of digits of a number until sum becomes single digit - GeeksforGeeks
Given a number n, we need to find the sum of its digits such that:
Read full article from Finding sum of digits of a number until sum becomes single digit - GeeksforGeeks
Given a number n, we need to find the sum of its digits such that:
If n < 10 digSum(n) = n Else digSum(n) = Sum(digSum(n))
int
digSum(
int
n)
{
return
(n % 9 == 0) ? 9 : (n % 9);
}
int
digSum(
int
n)
{
int
sum = 0;
while
(n > 0 || sum > 9)
{
if
(n == 0)
{
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
return
sum;
}