We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
intnum1(longlongintz){if(z<=9){returnz;}longlongintsum=0;longlongintm=0;while(z!=0){m=z%10;sum=sum+m;z=z/10;}returnnum1(sum);}// Complete the superDigit function below.intsuperDigit(stringn,intk){longlongintsum=0;longlongintm;for(inti=0;i<n.size();i++){sum+=n[i]-'0';}returnnum1(sum*k);}
Recursive Digit Sum
You are viewing a single comment's thread. Return to all comments →
Here's the C++ Code, All test cases passing
Can you explain why doesn't it overflow in test case 9?
hey when the range of n is so large then how did your code got get all test cases passed .
I avoided % and / and passed all tests in cpp.
can u explain if the n is string how u are converting it in int
use function stoi( )
using ASCII, Since all numerical characters will be in series in ASCII table. eg. '9' - '0' = 9 and so on
using ASCII value like this : (int)s[i]-'0'
Can you please explain why you called num1 function by passing sum*k