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

+14

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

Luca

36 Answers

New Answer

+23

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 https://code.sololearn.com/cX6szu5M3Lud/?ref=app

+22

updated: now satisfies the challenge requirement. original: Piece of cake for ruby! https://code.sololearn.com/cUiWb8ULQ2d9/?ref=app

+19

thnx 😊😊@luca

+18

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

+16

my c++ version πŸ˜ƒ https://code.sololearn.com/cxxCmyv3L0Xr/?ref=app

+11

https://code.sololearn.com/c6kj7a6iHqM0/?ref=app

+9

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

+8

Integers don't show a "0" in front

+7

@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 :)

+7

Will this do--> https://code.sololearn.com/cf7jMi7E1x4R/?ref=app

+7

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

+7

I'm curious @Gaurav 😁

+7

My try https://code.sololearn.com/cIBjhNz05g62/?ref=app

+6

I can try, but that 0 infront is not good

+6

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

+6

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)

+5

@Gaurav i like recursion 😁

+5

Not the regular split().reverse().join() . Not just numbers anything is reversed :) https://code.sololearn.com/Wjc93gIlCiba/?ref=app

+5

https://code.sololearn.com/c9RX6f34KcGR/?ref=app

+4

Ah I went to sleep and couldn't do it