Digital Root (repeated digital sum) of the given large integer - GeeksforGeeks

17 Aug, 2021
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.Given a number, the task is to find its digital root. The input number may be large and it may not be possible to store even if we use long long int.Asked in ACM-ICPCExamples :  
Input : num = "1234"
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum(x) == 10
              Hence ans will be 1+0 = 1

Input : num = "5674"
Output : 4 
We have discussed a solution for numbers that can fit in long long it in below post. Finding sum of digits of a number until sum becomes single digitIn this post, similar approaches are discussed for large numbers. 
 
Method 1
Find Digital root of 65785412 Steps:  
Find out all the digits of a numberAdd all the number one by oneIf the final sum is double digit, add again to make it single digitThe result obtained in single digit is the Digital root of number
Find out all the digits of a number
Add all the number one by one
If the final sum is double digit, add again to make it single digit
The result obtained in single digit is the Digital root of number
Example: Input: 65785412 Find Digital root: (6 + 5 + 7 + 8 + 5 + 4 + 1 + 2) = 38 => 11 => (1 + 1) = 2 Output: 2  
Method 2
The idea is based on the fact that for a non-zero number num, digital root is 9 if number is divisible by 9, else digital root is num % 9. (Please see http://www.sjsu.edu/faculty/watkins/Digitsum0.htm for details)Find the digital root of 65785412 Steps:  
Sum of digits = 6 + 5 + 7 + 8 + 5 + 4 + 1 + 2 = 38Since 38 is not multiple of 9, digital root is 38 % 9 = 2.
Sum of digits = 6 + 5 + 7 + 8 + 5 + 4 + 1 + 2 = 38
Since 38 is not multiple of 9, digital root is 38 % 9 = 2.
 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find  digital root of a number#include<bits/stdc++.h>using namespace std; // Returns digital root of numint digitalRoot(string num){    // If num is 0.    if (num.compare("0") == 0)        return 0;     // Count sum of digits under mod 9    int ans = 0;    for (int i=0; i<num.length(); i++)        ans = (ans + num[i]-'0') % 9;     // If digit sum is multiple of 9, answer    // 9, else remainder with 9.    return (ans == 0)? 9 : ans % 9;} // Driver codeint main(){    string num = "65785412";     // Calling digitalRoot function    cout<< digitalRoot(num) <<endl;     return 0;} // Note: Special case when num = "00..."// program will not give correct output.
// Java code for digital rootimport java.util.*;  public class GfG {          static int digroot(int n)    {        int root = 0;          // Loop to do sum while        // sum is not less than        // or equal to 9        while (n > 0 || root > 9)        {             if (n == 0) {                n = root;                root = 0;            }                         root += n % 10;            n /= 10;        }        return root;    }          // Driver code    public static void main(String argc[])    {        int n = 65785412;        System.out.println(digroot(n));    }} // This code is contributed by Gitanjali.// This code will run for 0000 testcase also.
# Python3 program to find digital root# of a numberimport math # Returns digital root of numdef digitalRoot(num):         # If num is 0.    if (num == "0"):        return 0     # Count sum of digits under mod 9    ans = 0    for i in range (0, len(num)):        ans = (ans + int(num[i])) % 9              # If digit sum is multiple of 9, answer    # 9, else remainder with 9.    if(ans == 0):        return 9    else:        return ans % 9 # Driver methodnum = "65785412" # Calling digitalRoot functionprint (digitalRoot(num)) # This code is contributed by Gitanjali.
// C# code for digital rootusing System; class GfG {         static int digroot(int n)    {        int root = 0;         // Loop to do sum while        // sum is not less than        // or equal to 9        while (n > 0 || root > 9)        {            if (n == 0) {                n = root;                root = 0;            }                         root += n % 10;            n /= 10;        }        return root;    }         // Driver code    public static void Main()    {        int n = 65785412;        Console.Write(digroot(n));    }} // This code is contributed by Smitha// This code will run for 0000 testcase also.
<?php// PHP program to find// digital root of a number // Returns digital root of numfunction digroot($n){    $root = 0;     // Loop to do sum while    // sum is not less than    // or equal to 9    while ($n > 0 || $root > 9)    {        if ($n == 0)        {            $n = $root;            $root = 0;        }                 $root += $n % 10;        $n /= 10;    }    return $root;} // Driver code$num = 65785412; // Calling digitalRoot functionecho digroot($num); // This code is contributed by Sam007?>
<script>    // Javascript code for digital root         function digroot(n)    {        let root = 0;           // Loop to do sum while        // sum is not less than        // or equal to 9        while (n > 0 || root > 9)        {            if (n == 0) {                n = root;                root = 0;            }                           root += n % 10;            n = parseInt(n / 10, 10);        }        return root;    }         let n = 65785412;      document.write(digroot(n));     </script>
Output :  
2
This article is contributed by Shiv Pratap Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Smitha Dinesh Semwal
Sam007
divyeshrabadiya07
saqibansariprime
large-numbers
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Prime Numbers
Modulo Operator (%) in C/C++ with Examples
Find all factors of a natural number | Set 1
Modulo 10^9+7 (1000000007)
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Minimum number of jumps to reach end
Operators in C / C++