+ 1

Help me with C

Does somebody know how I can get a number from user and reverse it?for example:45 chances to 54

6th Dec 2020, 4:54 AM
Ruby Parker
Ruby Parker - avatar
3 ответов
+ 3
let `n` be integer input. and `rev` be the integer initialized to 0. while n! =0 rev *= 10 rev += n%10 n/=10 return rev what this loop does is : it multiplies the `rev` by 10 and stores result back into `rev`. then adds the last digit (retrieved by mod division n%10) to `rev`. next, it removes last digit of input number by using n/=10. all this while n! =0. example : n = 45 rev = 0 in while, check n! =0 , this is true so execute loop body. rev = 0 *10 = 0 rev = 0 + 45%10 = 0 + 5 = 5 n = 45/ 10 = 4 so after first iteration `rev` is 5 and `n` is 4 next iteration - check while, n! =0. true so execute loop body. rev = 5*10 = 50 rev = 50 + (4%10) = 50 +4 = 54 n = 4/10 = 0 now `rev` is 54 and n = 0. check while, n! =0 is false. loop body won't execute. return value of `rev` which is 54 Try to code this logic as C function. If something doesn't work , ask here.
6th Dec 2020, 5:09 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
0
Don't store it as a number. Store it as a string and reverse the string.
6th Dec 2020, 4:55 AM
你知道規則,我也是
你知道規則,我也是 - avatar