Chewbacca And Numbers

How to handle the case when we have number like this 999, 9 etc.

Hi @Vikaspal
store the number as string. run a while loop till s[i]==‘9’ and after that solve the question as stated.
Hope dis resolves ur doubt

for another number its works fine

int main() {
long int num;
int rem;
long int ans=0;
int mul=1;
cin >> num;
while(num != 0) {
// addeding the remainder and ones, tens digits. each time increment
rem = num%10;
if(rem >=5 && rem != 0) {
rem = 9 - rem;
}
ans = ans + remmul;
mul = mul
10;
num = num/10; // take the next element/character
//cout << num << " ";
}
cout << endl << ans;
return 0;
}

In ur code, when u are acquiring 9 ur code is converting it to 0. so when u give 9991 as input, it convert 9991 to 0001 and the output comes out to be 1.
To avoid dis, u can first store 999 in ur answer and then perform all ur operations.
eg. input=99911289
so first store 999 in ans. and then perform all ur operation on d code which will give 99911210 as output.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.