Digital Root of a given large integer using Recursion | 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
function convertToString ( sum ) { let str = " " ; while ( sum > 0 ) { str = str + String . fromCharCode ( ( sum % 10 ) + ' ' . charCodeAt ( ) ) ; sum = parseInt ( sum / 10 , 10 ) ; } return str ; } function GetIndividulaDigitSum ( str , len ) { let sum = 0 ; for ( let i = 0 ; i < len ; i ++ ) { sum = sum + str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; } return convertToString ( sum ) ; } function GetDigitalRoot ( str ) { if ( str . length == 1 ) { return ( str [ 0 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; } str = GetIndividulaDigitSum ( str , str . length ) ; return GetDigitalRoot ( str ) ; } let str = " " ; document . write ( GetDigitalRoot ( str ) ) ;