Digital Root of a given large integer using Recursion - GeeksforGeeks

25 Oct, 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 large number N, 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.Examples: 
Input: N = 675987890789756545689070986776987 Output: 5 Explanation: Sum of individual digit of the above number = 212 Sum of individual digit of 212 = 5 So the Digital root is 5Input: num = 876598758938317432685778263 Output: 2 Explanation: Sum of individual digit of the above number = 155 Sum of individual digit of 155 = 11 Sum of individual digit of 11 = 2 So the Digital root is 2  
Approach:  
Find out all the digits of a number.Add all the number one by one.If the final sum contains more than one digit, Call the recursive function again to make it a single digit.The result obtained in the single-digit is the Digital Root of the number.
Find out all the digits of a number.
Add all the number one by one.
If the final sum contains more than one digit, Call the recursive function again to make it a single digit.
The result obtained in the single-digit is the Digital Root of the number.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to print the digital// root of a given very large number #include <iostream>using namespace std; // Function to convert given// sum into stringstring convertToString(int sum){    string str = "";    // Loop to extract digit one by one    // from the given sum and concatenate    // into the string    while (sum) {        // Type casting for concatenation        str = str + (char)((sum % 10) + '0');        sum = sum / 10;    }    // Return converted string    return str;} // Function to get individual digit// sum from stringstring GetIndividulaDigitSum(string str,                             int len){    int sum = 0;    // Loop to get individual digit sum    for (int i = 0; i < len; i++) {        sum = sum + str[i] - '0';    }    // Function call to convert    // sum into string    return convertToString(sum);} // Function to calculate the digital// root of a very large numberint GetDigitalRoot(string str){    // Base condition    if (str.length() == 1) {        return str[0] - '0';    }    // Function call to get    // individual digit sum    str = GetIndividulaDigitSum(        str,        str.length());    // Recursive function to get digital    // root of a very large number    return GetDigitalRoot(str);}int main(){    string str        = "675987890789756545689070986776987";     // Function to print final digit    cout << GetDigitalRoot(str);}
// Java program to print the digital// root of a given very large numberclass GFG{  // Function to convert given// sum into Stringstatic String convertToString(int sum){    String str = "";     // Loop to extract digit one by one    // from the given sum and concatenate    // into the String    while (sum > 0) {         // Type casting for concatenation        str = str + (char)((sum % 10) + '0');        sum = sum / 10;    }     // Return converted String    return str;}  // Function to get individual digit// sum from Stringstatic String GetIndividulaDigitSum(String str,                             int len){    int sum = 0;     // Loop to get individual digit sum    for (int i = 0; i < len; i++) {        sum = sum + str.charAt(i) - '0';    }     // Function call to convert    // sum into String    return convertToString(sum);}  // Function to calculate the digital// root of a very large numberstatic int GetDigitalRoot(String str){    // Base condition    if (str.length() == 1) {        return str.charAt(0) - '0';    }     // Function call to get    // individual digit sum    str = GetIndividulaDigitSum(        str,        str.length());     // Recursive function to get digital    // root of a very large number    return GetDigitalRoot(str);} // Driver codepublic static void main(String[] args){    String str        = "675987890789756545689070986776987";      // Function to print final digit    System.out.print(GetDigitalRoot(str));}} // This code is contributed by sapnasingh4991
# Python3 program to print the digital# root of a given very large number # Function to convert given# sum into stringdef convertToString(sum):    str1 = ""     # Loop to extract digit one by one    # from the given sum and concatenate    # into the string    while (sum):         # Type casting for concatenation        str1 = str1 + chr((sum % 10) + ord('0'))        sum = sum // 10     # Return converted string    return str1 # Function to get individual digit# sum from stringdef GetIndividulaDigitSum(str1, len1):    sum = 0    # Loop to get individual digit sum    for i in range(len1):        sum = sum + ord(str1[i]) - ord('0')     # Function call to convert    # sum into string    return convertToString(sum) # Function to calculate the digital# root of a very large numberdef GetDigitalRoot(str1):    # Base condition    if (len(str1) == 1):        return ord(str1[0] ) - ord('0')     # Function call to get    # individual digit sum    str1 = GetIndividulaDigitSum(str1,len(str1))    # Recursive function to get digital    # root of a very large number    return GetDigitalRoot(str1) if __name__ == '__main__':    str1 = "675987890789756545689070986776987"     # Function to print final digit    print(GetDigitalRoot(str1)) # This code is contributed by Surendra_Gangwar
// C# program to print the digital// root of a given very large numberusing System;class GFG{  // Function to convert given// sum into Stringstatic String convertToString(int sum){  String str = "";   // Loop to extract digit one by one  // from the given sum and concatenate  // into the String  while (sum > 0)  {    // Type casting for concatenation    str = str + (char)((sum % 10) + '0');    sum = sum / 10;  }   // Return converted String  return str;}  // Function to get individual digit// sum from Stringstatic String GetIndividulaDigitSum(String str,                                    int len){  int sum = 0;   // Loop to get individual digit sum  for (int i = 0; i < len; i++)  {    sum = sum + str[i] - '0';  }   // Function call to convert  // sum into String  return convertToString(sum);}  // Function to calculate the digital// root of a very large numberstatic int GetDigitalRoot(String str){  // Base condition  if (str.Length == 1)  {    return str[0] - '0';  }   // Function call to get  // individual digit sum  str = GetIndividulaDigitSum(str,                              str.Length);   // Recursive function to get digital  // root of a very large number  return GetDigitalRoot(str);} // Driver codepublic static void Main(String[] args){  String str =         "675987890789756545689070986776987";   // Function to print readonly digit  Console.Write(GetDigitalRoot(str));}} // This code is contributed by Rajput-Ji
<script>    // Javascript program to print the digital    // root of a given very large number         // Function to convert given    // sum into string    function convertToString(sum)    {        let str = "";        // Loop to extract digit one by one        // from the given sum and concatenate        // into the string        while (sum > 0)        {                     // Type casting for concatenation            str = str + String.fromCharCode((sum % 10) + '0'.charCodeAt());            sum = parseInt(sum / 10, 10);        }        // Return converted string        return str;    }     // Function to get individual digit    // sum from string    function GetIndividulaDigitSum(str, len)    {        let sum = 0;                 // Loop to get individual digit sum        for (let i = 0; i < len; i++) {            sum = sum + str[i].charCodeAt() - '0'.charCodeAt();        }                 // Function call to convert        // sum into string        return convertToString(sum);    }     // Function to calculate the digital    // root of a very large number    function GetDigitalRoot(str)    {        // Base condition        if (str.length == 1) {            return (str[0].charCodeAt() - '0'.charCodeAt());        }                 // Function call to get        // individual digit sum        str = GetIndividulaDigitSum(str, str.length);                 // Recursive function to get digital        // root of a very large number        return GetDigitalRoot(str);    }         let str = "675987890789756545689070986776987";      // Function to print final digit    document.write(GetDigitalRoot(str)); // This code is contributed by mukesh07.</script>
5
 
SURENDRA_GANGWAR
sapnasingh4991
Akanksha_Rai
Rajput-Ji
anikaseth98
simranarora5sos
mukesh07
ACM-ICPC
large-numbers
number-digits
root
Mathematical
Recursion
Strings
Strings
Mathematical
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction
Write a program to reverse digits of a number