Digital Root of a given large integer using Recursion | C ++ 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
#include <iostream> NEW_LINE using namespace std ; string convertToString ( int sum ) { string str = " " ; while ( sum ) { str = str + ( char ) ( ( sum % 10 ) + '0' ) ; sum = sum / 10 ; } return str ; } string GetIndividulaDigitSum ( string str , int len ) { int sum = 0 ; for ( int i = 0 ; i < len ; i ++ ) { sum = sum + str [ i ] - '0' ; } return convertToString ( sum ) ; } int GetDigitalRoot ( string str ) { if ( str . length ( ) == 1 ) { return str [ 0 ] - '0' ; } str = GetIndividulaDigitSum ( str , str . length ( ) ) ; return GetDigitalRoot ( str ) ; } int main ( ) { string str = "675987890789756545689070986776987" ; cout << GetDigitalRoot ( str ) ; }