Challenge: reverse an integer! | Sololearn: Learn to code for FREE!


Challenge: reverse an integer!

hi! here there is a new challenge for you: reverse an integer! example: input: 1234567890 output: 0987654321 i know, it is quite an easy challenge... but to make things a little more difficult you can only use math operations! built in functions, arrays, vectors or similar must not be used (easy anyway, don't worry :D) code in any language you want and show us your skills! happy coding :)

10/14/2017 5:31:15 AM


36 Answers

New Answer


try it guys ☺ //works for every integer , doesn't matter wherther u put 0's in front or u enter negative integer //by using 2 methods ::: loops and recursion ☺ //exceptions handled


updated: now satisfies the challenge requirement. original: Piece of cake for ruby!


thnx 😊😊@luca


JavaScript: alert(prompt("").split("").reverse().join(""));


my c++ version πŸ˜ƒ



num = int(input("Enter an integer to reverse: ")) while num != 0: print(num % 10, end='') num //= 10


Integers don't show a "0" in front


@Pegasus you can skip leading zeros if you prefer @Lord nice code, but the task is to use only math operators and not built in functions or vectors/arrays/similar :)


Will this do-->


@ChaoticDawg excellent solution, best one till now! exactly the challenge task!


I'm curious @Gaurav 😁


My try


I can try, but that 0 infront is not good


JS: alert( (1234567890+'').split('').reverse().join('') )


Slight modification to my previous code that actually reverses the integer as a whole instead of printing out each individual digit in the loop: num = int(input("Enter an integer to reverse: ")) rev_num = 0 while num != 0: rev_num = (rev_num * 10) + (num % 10) num //= 10 print(rev_num)


@Gaurav i like recursion 😁


Not the regular split().reverse().join() . Not just numbers anything is reversed :)



Ah I went to sleep and couldn't do it