You are viewing a single comment's thread. Return to all comments →
My C solution : ( the long long to array part was not my part )
void reverse(char str[], int length) { int start = 0; int end = length - 1; while (start < end) { char temp = str[start]; str[start] = str[end]; str[end] = temp; end--; start++; } } // Implementation of lltoa() char* llitoa(long long num, char* str, int base) { unsigned int i = 0; /* Handle 0 explicitly, otherwise empty string is * printed for 0 */ if (num == 0) { str[i++] = '0'; str[i] = '\0'; return str; } // Process individual digits while (num != 0) { long long rem = num % base; str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0'; num = num / base; } str[i] = '\0'; // Append string terminator // Reverse the string reverse(str, i); return str; } int superDigit(char* n, int k) { long long sum = 0; int loop = 0; for(loop = 0 ; loop < strlen(n) ; loop++) { sum += (n[loop] - 48); // ASCII } if(k > 0) { sum *= (k) ; } llitoa(sum, n, 10); if(sum> 9) { sum = superDigit(n,0); } return sum ; }
Seems like cookies are disabled on this browser, please enable them to open this website
Recursive Digit Sum
You are viewing a single comment's thread. Return to all comments →
My C solution : ( the long long to array part was not my part )