https://ide.codingblocks.com/s/163749 why do we need to convert character into integer and why this code is wrong?
Chewbacaa and numbers
Hello @nirupama1,
-
You need not convert the character to an integer.
You can directly apply the same logic to characters also as each element of your char array is a separate digit. -
Your code has a logical mistake, which is producing the wrong output:
c[i]=‘9’-c[i];
Suppose when c[i]=‘5’,
‘9’-c[i] = ‘9’-‘5’ = 57-53(ASCII values) = 4 = EOT(corresponding character in ASCII series)
Hence, producing a wrong output.
Solution:
c[i]=‘9’-c[i]+‘0’;
For same example,
‘9’-c[i]+‘0’ = ‘9’-‘5’+‘0’ = 57-53+48(ASCII values) = 52 = ‘4’(corresponding character in ASCII series)
Hence, desired output.
Modified Code:
Hope, this would help.
Give a like if you are satisfied.