Repetitive Addition Of Digits | Practice | GeeksforGeeks

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)
Constraints:
1 <= N <= 1018
 
0
minipandey3326 days ago
C++ Simple easy logic based solution :-
 
 int singleDigit(long long N)   {       int sum=0;       // Write Your Code here       while(1)       {                     sum = sum + (N%10);   //1 2 3 4 = 10 = 1            N=N/10;                      if(N==0 && sum>9)             {                 N=sum; sum=0;             }                      if(N==0 && sum<=9) break;       }              return sum;   }
0
kirannaidu80551 week ago
Java :
class Solution{   static int singleDigit(long N){   long sum = 0; if (N < 10) {  System.out.println("its a single digit"); } else {  while (N >= 10) {   sum = (N / 10) + (N % 10);   N = sum;
  } } return (int)sum;}}
0
mayank20212 weeks ago
C++ : 0.01/1.34int singleDigit(long long N)   {       long long N1=N, sum=0;       while(N1)       {           sum=sum+N1%10;           N1=N1/10;       }       if(sum<=9) return sum;       else return singleDigit(sum);   }
+2
sarthakgupta102 months ago
class Solution
{
public:
    int singleDigit(long long N)
    {
        // Write Your Code here
        return 1+(N-1)%9;
    }
};
0
rishalirishu772 months ago
The Simplest Code:long long sum=0;       while(N)       {           sum+=(N%10);           N/=10;       }       if(sum<10)       {           return sum;       }       else       return singleDigit(sum);
0
simplykiran0077 months ago
import math
# method to find sum of digits
# of a number until sum becomes
# single digit
def digSum( n):
    sum = 0
     
    while(n > 0 or sum > 9):
     
        if(n == 0):
            n = sum
            sum = 0
         
        sum += n % 10
        n /= 10
     
    return sum
# Driver method
n = 1234
print (digSum(n))
+4
Abhishek Raj8 months ago
Abhishek Raj
Time complexity: O(1) , Space Complexity: O(1)
class Solution{    static int singleDigit(long num){        // code here        if (num == 0) return 0;        if (num % 9 == 0) return 9;        num = num % 9;        return (int)num;    }}
0
Sagar Sikchi9 months ago
Sagar Sikchi
This question has been asked in VMware as well :)
Java Solution:
class Solution{    static int singleDigit(long N){        while(N >= 10) {            long sum = 0;            while(N > 0) {                sum += N % 10;                N /= 10;            }            N = sum;        }        return (int) N;    }}
0
chandan kumar9 months ago
chandan kumar
JAVA RECURSION SOLUTION
class Solution{    static int singleDigit(long N){        // code here
         if(N<=0)        return 0;
        long res = (int)singleDigit(N/10) + (int)(N%10);        if(res<10)        return (int)res;
        else        return singleDigit(res);    }}
0
Aditya Anerao9 months ago
Aditya Anerao
int singleDigit(long long N)    {        // Write Your Code here        if(N<10){            return N;        }        int num,res=0;        while(N>0){            if(N<10){                return N;            }            num=N%10;            res=res+num;            N=N/10;            if(N<10){                res=res+N;                N=res;                res=0;            }          }    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.