Why isn't this code running ?problem code:CIELAB

a,b=map(int,input().split())
result=abs(a-b)
res=str(result)
if(res[-1]==0):
result+=1
else:
result-=1

print(result)

try this
200 100
difference is 100 and you have to modify only one digit here to get the right answer.

But your code gives 99 an answer, modify the string(res) not the difference(result). as number of digit can’t change.

here is the modified code

a,b=map(int,input().split())
result=abs(a-b)
res=str(result)
if(res[-1]=='1'):
    res = res[:len(res)-1]+"2"
else:
    res = res[:len(res)-1]+"1"
print(res)

thank you

but this gives 101 as an output right???because res[-1]==0 if block executes and it increments by one and not 99

What about this?
#include <bits/stdc++.h>
using namespace std;

int main() {
int a,b;
cin>>a>>b;
int d=abs(a-b);
d=d/10;
if(d!=0)
cout<<d<<“1\n”;
else
cout<<“1\n”;
return 0;
}
it gives wa…

It doesn’t increment any thing it just changes the last digit.

What needs to be incremented?
The question is to change any one digit

try this
201 100

your code will not work when difference has last digit = 1

1 Like

finally got what needs to be incremented, xD
#include <bits/stdc++.h>
using namespace std;

int main() {
int a,b;
cin>>a>>b;
int d=abs(a-b);
int left=d%10;
if(left<=1)left++;
else left–;
d=d/10;
if(d!=0)
cout<<d<<left<<"\n";
else
cout<<left<<"\n";
return 0;
}
Thanks bro