int ans=0;
int p=1;
for(int i=s.length()-1;i>=0;i–)
{
ans+=((s[i]-‘0’)p);
p=p10;}
Why have we subtracted ‘0’ from s[i]?
int ans=0;
int p=1;
for(int i=s.length()-1;i>=0;i–)
{
ans+=((s[i]-‘0’)p);
p=p10;}
Why have we subtracted ‘0’ from s[i]?
@isingh, here what happens is when you subtract/add two character then actually you are subtracting/adding their ascii values as subtraction/addition operation is not defined for the characters so character is typecasted to int by compiler which returns in their ascii value
if s[i] is 8 , then (s[i] - ‘0’) will be computed as (56 - 48) (ascii code for ‘8’ is 56 and ‘0’ is 48) so we get value as 8 , so this a way to convert a character digit value to integer digit value