Recursively add the digits of a Number and stop when it has become a single digit

Given an Integer, add all the digits and check whether it has become a single digit or not. If it does, display the single digit you got or repeat the adding digits until it becomes a single digit and display the single digit.



For Example:

If the Integer given is 167 = 1 + 6 + 7 = 14 = 1 + 4 = 5 ( it has became a single digit) . Hence, stop adding the digits and the output is "5"


Implementation:


// Following code is the C implementation

#include <stdio.h>

int main()
{
    unsigned long long int T,N,sum;
    
    /* Get the Number of Test Cases */
    scanf("%llu",&T); 
    
    /* Loop for each Test Case */
    while(T--)
    {
          /* Input Number which has to be converted */
          scanf("%llu",&N); 
           
          /* Loop until there exists at least a single digit in N */
          while(N / 10 != 0)
          {
                /* Initialize sum as "0" */
                sum = 0;

                /* If N=123, 
                a. N%10 which is "3" will be added to sum in the first iteration
                b. N becomes N=N/10, which removes "3" from original number "123", so , now, N=12
                c. Update sum = new N value. so, now, sum = 12
                d. Repeat the above process until there exists at least a single digit. Stop the iteration when it does.
                */
                for(;N != 0;N = N / 10)
                sum += N % 10;
                N = sum;
          }
          /* Print the sum which is a single digit */
          printf("%llu\n",sum);
    }
    return 0;
}
Sample Input:

3
167
569
102

Sample Output:

5
2
3