Repetitive Addition Of Digits

 Given a non-negative integer N, repeatedly add all its digits until the result has only one digit.

Example 1:

Input:
N = 98

Output:
8

Explanation:
Sum of digit of N = 98  is 9 + 8 = 17
Sum of digit of 17 = 1 + 7 = 8
Since 8 is the single digit number hence it is the 
required answer.

Example 2:

Input:
N = 1

Output:
1

Explanation:
Since 1 is the single-digit number hence it is the 
required answer.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function singleDigit() which takes an integer N as an input parameter and return the single-digit you will get after repeatedly adding the digits of N.

Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)



Answer : 


class Solution

{

public:

    int singleDigit(long long N)

    {   long long x = N;

        int sum = 0;

    

        while (x )

        {   sum += (x%10);

            x = x/10;

            if( (!x)   && (sum/10 != 0))

            {

                x = sum ;

                sum = 0;

            }

            

        }

        return sum;

    }

};



references:  https://practice.geeksforgeeks.org/problems/repetitive-addition-of-digits2221/1/#

Comments