reverse the position of integer digits?

后端 未结 13 2520
予麋鹿
予麋鹿 2021-02-09 15:00

i have to reverse the position of integer like this

input = 12345

output = 54321

i made this but it gives wrong output e.g 5432

#include         


        
13条回答
  •  余生分开走
    2021-02-09 15:20

    If you try it once as an example, you'll see your error.

    Input: 12

    first loop:

    out: 12%10 = 2 / 1 = 2
    i = 100
    test: 12/100 = 0 (as an integer)

    aborts one too early.

    One solution could be testing

    (num % i) != num

    Just as one of many solutions.

提交回复
热议问题