That's why I added the code comments, for understanding. Its just maths. It makes me think you don't understand what a modulo operation is?
The method is to do one digit at a time from the integer until nothing is left.
From the input number, get the smallest to biggest digit using modulo 10, and divide by 10 to shift the integer down.
For the reversed result, you do the opposite. Use the digits acquired above but add them to the result after multiplying by 10 for each one.
Example 345, need to get the 5, use module 10. Then need the 4, so take 345/10 = 34, and module 10 = 4. Need the 3, so take 34/10 mod 10 = 3.
For the result you just do the opposite with those numbers. 5, then when you have the 4, take 5*10+4=54, when you have the 3, 54*10+3.
Example: Start x = 345, res=0
First loop:
- digit = x % 10 = 5
- temp = res * 10 + digit = 0*10 + 5 = 5
- if ((temp - digit) / 10 != res) = if ((5-5)/10 != 0) = if (0!=0)
- res = temp = 5
- x/=10 = x/10 = 345/10 = 34
Second loop:
- digit = x % 10 = 34%10 = 4
- temp = res * 10 + digit = 5*10 + 4 = 54
- if ((temp - digit) / 10 != res) = if ((54-4)/10 != 5) = if (5!=5)
- res = temp = 54
- x/=10 = x/10 = 34/10 = 3
Third loop:
- digit = x % 10 = 3%10 = 3
- temp = res * 10 + digit = 54*10 + 3 = 543
- if ((temp - digit) / 10 != res) = if ((543-3)/10 != 54) = if (54!=54)
- res = temp = 543
- x/=10 = x/10 = 3/10 = 0
No forth loop because x=0
return result = res = 543