Digital Root of a given large integer using Recursion | Java program to print the digital root of a given very large number ; Function to convert given sum into String ; Loop to extract digit one by one from the given sum and concatenate into the String ; Type casting for concatenation ; Return converted String ; Function to get individual digit sum from String ; Loop to get individual digit sum ; Function call to convert sum into String ; Function to calculate the digital root of a very large number ; Base condition ; Function call to get individual digit sum ; Recursive function to get digital root of a very large number ; Driver code ; Function to print final digit
class GFG { static String convertToString ( int sum ) { String str = " " ; while ( sum > 0 ) { str = str + ( char ) ( ( sum % 10 ) + '0' ) ; sum = sum / 10 ; } return str ; } static String GetIndividulaDigitSum ( String str , int len ) { int sum = 0 ; for ( int i = 0 ; i < len ; i ++ ) { sum = sum + str . charAt ( i ) - '0' ; } return convertToString ( sum ) ; } static int GetDigitalRoot ( String str ) { if ( str . length ( ) == 1 ) { return str . charAt ( 0 ) - '0' ; } str = GetIndividulaDigitSum ( str , str . length ( ) ) ; return GetDigitalRoot ( str ) ; } public static void main ( String [ ] args ) { String str = "675987890789756545689070986776987" ; System . out . print ( GetDigitalRoot ( str ) ) ; } }