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

14th Oct 2017, 5:31 AM
Luca
Luca - avatar
36 odpowiedzi
+ 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
14th Oct 2017, 7:03 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 22
updated: now satisfies the challenge requirement. original: Piece of cake for ruby! https://code.sololearn.com/cUiWb8ULQ2d9/?ref=app
14th Oct 2017, 5:48 AM
Lord Krishna
Lord Krishna - avatar
+ 19
thnx 😊😊@luca
14th Oct 2017, 7:09 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 18
JavaScript: alert(prompt("").split("").reverse().join(""));
14th Oct 2017, 6:43 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
14th Oct 2017, 1:58 PM
Vukan
Vukan - avatar
+ 11
https://code.sololearn.com/c6kj7a6iHqM0/?ref=app
14th Oct 2017, 6:58 AM
qwerty
qwerty - avatar
+ 9
num = int(input("Enter an integer to reverse: ")) while num != 0: print(num % 10, end='') num //= 10
14th Oct 2017, 6:15 AM
ChaoticDawg
ChaoticDawg - avatar
+ 8
Integers don't show a "0" in front
14th Oct 2017, 5:38 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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 :)
14th Oct 2017, 5:57 AM
Luca
Luca - avatar
14th Oct 2017, 6:04 AM
AlphaCoder
AlphaCoder - avatar
+ 7
@ChaoticDawg excellent solution, best one till now! exactly the challenge task!
14th Oct 2017, 6:18 AM
Luca
Luca - avatar
+ 7
I'm curious @Gaurav 😁
14th Oct 2017, 6:25 AM
Luca
Luca - avatar
14th Oct 2017, 11:36 AM
David Akhihiero
David Akhihiero - avatar
+ 6
I can try, but that 0 infront is not good
14th Oct 2017, 5:37 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
JS: alert( (1234567890+'').split('').reverse().join('') )
14th Oct 2017, 6:17 AM
Calviղ
Calviղ - avatar
+ 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)
14th Oct 2017, 6:47 AM
ChaoticDawg
ChaoticDawg - avatar
+ 5
@Gaurav i like recursion 😁
14th Oct 2017, 7:04 AM
Luca
Luca - avatar
+ 5
Not the regular split().reverse().join() . Not just numbers anything is reversed :) https://code.sololearn.com/Wjc93gIlCiba/?ref=app
14th Oct 2017, 1:54 PM
Kash
Kash - avatar
+ 5
https://code.sololearn.com/c9RX6f34KcGR/?ref=app
14th Oct 2017, 3:21 PM
Sudipta Kumar Nath
Sudipta Kumar Nath - avatar
+ 4
Ah I went to sleep and couldn't do it
14th Oct 2017, 7:13 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar