| Bytes | Lang | Time | Link |
|---|---|---|---|
| 004 | Uiua | 241123T192258Z | ErikDaPa |
| nan | 241123T194023Z | amir hom | |
| 025 | AWK | 241122T203539Z | xrs |
| 006 | Juby | 241122T145908Z | Jordan |
| 003 | Pyth | 150622T174839Z | cmxu |
| nan | mIRC 4.45 35 Bytes | 160604T102347Z | O S |
| 034 | Oracle SQL 11.2 | 160603T142656Z | Jeto |
| 033 | Python 3 | 160603T142045Z | m654 |
| 026 | Python 2 | 160603T135810Z | Erik the |
| 003 | MATL | 160124T051407Z | Luis Men |
| 002 | Burlesque | 151022T170317Z | mroman |
| 014 | Mathematica | 151022T165312Z | Martin E |
| nan | 151022T164455Z | letsch32 | |
| 054 | TIBASIC | 140408T155156Z | Timtech |
| 031 | ES6 Javascript | 150704T143121Z | Afonso M |
| 058 | C# | 150704T044404Z | Cooler R |
| 023 | Julia | 150703T215743Z | M L |
| 3128 | TIBASIC | 150622T233514Z | user3932 |
| 025 | POWERSHELL | 150623T065139Z | blabb |
| 020 | rs | 150623T020211Z | kirbyfan |
| 003 | K | 150622T181503Z | JohnE |
| 011 | Brainfuck | 140702T130348Z | rpax |
| 035 | JavaScript ES6 | 140227T055758Z | Eliseo D |
| 029 | Rebol | 140227T222754Z | draegtun |
| nan | 110831T192407Z | Ed Staub | |
| 032 | Groovy | 131202T091137Z | Carlos G |
| 023 | Haskell | 131201T004904Z | Thom Wig |
| nan | 130110T193148Z | Pjot | |
| 044 | Game Maker Language | 131130T161155Z | Timtech |
| nan | 131130T153711Z | FireFly | |
| nan | 110611T105522Z | Kiril Ki | |
| 060 | Common Lisp | 121230T215645Z | Chris Zi |
| nan | 121229T162557Z | TwiN | |
| 035 | R | 121202T132244Z | Ari B. F |
| nan | 121201T074859Z | tusharma | |
| 003 | Befunge | 121201T022037Z | daniero |
| 014 | Ruby | 110611T124556Z | bodaciou |
| 009 | PHP | 110611T124215Z | powtac |
| 037 | PHP | 120501T195550Z | l0n3sh4r |
| 003 | APL | 120501T175353Z | marinus |
| 026 | ruby | 120312T091243Z | jsvnm |
| 004 | k3 | 120311T215419Z | Adam Sch |
| 020 | in Q | 120307T173819Z | sinedcm |
| nan | 120308T190446Z | marinus | |
| 005 | Golfscript | 110830T191351Z | Peter Ta |
| 001 | HTML 7 chars | 110819T150917Z | Griffin |
| 075 | Scheme | 110612T165945Z | kba |
| 042 | Clojure | 111129T040501Z | Omar |
| 011 | Perl | 111126T142948Z | Zaid |
| nan | 111124T121553Z | Armand | |
| 019 | Perl | 110819T190832Z | Dynamic |
| 006 | k4 | 110830T222500Z | frank |
| nan | 110831T152045Z | Kumsal O | |
| nan | This one ACTUALLY takes an input | 110830T184251Z | Exelian |
| 017 | Vim | 110819T084245Z | Eric For |
| nan | 110621T204147Z | Alex | |
| nan | 110616T190116Z | JAB | |
| 006 | Perl | 110613T034710Z | Ming-Tan |
| nan | 110611T114913Z | Vader | |
| 033 | Scala 33 Chars | 110612T112754Z | Lalith |
| nan | 110611T144147Z | Dan Dipl | |
| 2824 | Haskell | 110611T113250Z | hammar |
| 006 | J | 110611T123503Z | MPelleti |
| nan | 110611T105441Z | RobG | |
| nan | 110611T111638Z | Victor | |
| nan | this is AS3 code | 110611T105848Z | pico.cre |
| nan | It is possible to convert a number a string | 110611T110020Z | taskinoo |
Uiua, 4 bytes
⍜°⋕⇌
Explanation:
⍜°⋕⇌
⍜ => under (do 1st func, then the 2nd, then undo the 1st)
°⋕ => number to string (1st func), undo func -> string back to number
⇌ => reverse (2nd func)
Python
def ans(n):
L = str(n)
return(int(L[::-1]))
mIRC 4.45 (35 Bytes)
$regsubex(12,/(.)/g,$mid(\A,-\n,1))
Oracle SQL 11.2, 35 34 bytes
SELECT 0+REVERSE(:1||'')FROM DUAL;
Python 3, 33 bytes
I decided to make a function instead of hardcoding it.
def r(n):print(int(str(n)[::-1]))
The code is pretty self-explanatory.
Usage: r(12345)
Python 2, 26 bytes
print int(`input()`[::-1])
MATL, 3 bytes (non-competing)
VPU
This uses release 10.1.0 of the language/compiler, which is the current version at the time of writing.
Explanation
V % implicitly input a number and convert to string
P % flip
U % convert to number and implicitly display
Burlesque, 2 bytes
Certain built-ins treat numbers as list of digits. The reverse built-in for example treats integers as a list of digits and thus reverses integers.
blsq ) 76543<-
34567
(For characters <- switches case).
Mathematica, 14 bytes
IntegerReverse
This is not competing, because this function was only added in last week's 10.3 release, but for completeness I thought I'd add the only ever (I think?) built-in for this task.
C++:
int reverse(int number, int number1, int number2){std::vector<int> v; v.push_back(number); v.push_back(number1); v.push_back(number2); std::cout << v[2] << v[1] << v[0]; return 0;}
TI-BASIC, 58 54
Input N:0:While N>0:Ans+E3fpart(N,10:N/10→N:End:Ans
Usage
prgmREVERSE
?598028
820895
Done
ES6 Javascript, 31 bytes
x=>[...x+''].reverse().join('')
C# - 58
I think we need more C#
int.Parse(string.Concat(Enumerable.Reverse(x.ToString())))
Julia, 23
r(x)=int(reverse("$x"))
$ interpolates an argument (or the result of a function) into a string, denoted by the double quotes. int() automatically deletes leading zeroes.
Example:
julia> x=1234567890;r(x)
987654321
or
julia> r(1234567890)
987654321
TI-BASIC, 35 32 31 (or 28) bytes
Takes input from the calculator's answer variable, which is whatever was last evaluated (like _ in the interactive python shell).
fPart(.1int(Ans10^(seq(A,A,~int(log(Ans+.5)),not(Ans
sum(Ans10^(cumSum(1 or Ans
10^( is 2NDLOG and ~ is ( - ), next to ENTER. Everything else can be found in the 2ND0 catalog.
If the program doesn't have to handle the possibility of 0 as input, it can be reduced to 28 bytes by changing ~int(log(Ans+.5)),not(Ans to ~int(log(Ans)),0.
Credit goes partially to Thomas Kwa for helping to golf this.
POWERSHELL, 25
-join(Read-Host)[-1..-9]
a longer version to take arbitrary input , 36
-join($a=Read-Host)[-1..-$a.length]
usage
PS C:\> -join(Read-Host)[-1..-9]
123456789
987654321
PS C:\>
rs, 20 bytes
#
+#(.*)(.)/\2#\1
#/
Technically, this doesn't count (rs was created earlier this year), but I didn't see any other regex-based answers, and I thought this was neat.
Explanation:
#
Insert a pound character at the beginning of the string. This is used as a marker.
+#(.*)(.)/\2#\1
Continuously prepend the last character of the main string to the area before the marker until there are no characters left.
#/
Remove the marker.
K, 3 bytes:
.|$
Evaluate (.) the reverse (|) of casting to a string ($).
Usage example:
.|$76543
34567
Brainfuck, 11
>,[>,]<[.<]
JavaScript (ES6) 35
a=b=>b.split("").reverse().join("")
Rebol, 29
f: func[n][do reverse mold n]
Usage example in Rebol console:
>> f 76543
== 34567
If you only wanted this to work on integer input then....
f: func [n [integer!]] [do reverse mold n]
Java
""+new StringBuilder(""+i).reverse();
Groovy 32
def r(n){print ((n+"").reverse())}
Haskell, 61,23
Using the string reversing technique. digitToInt is available from the Data.Char library, and of course this code would be shorter if we'd be able to assume if Data.Char already was available.
-- original version 61 chars
foldl1((+).(*)10).map(Data.Char.digitToInt)$reverse$show 1234
--bonus foldonly version, 67 chars
foldl1((+).(*)10).foldl(flip((:).Data.Char.digitToInt))[]$show 1234
--FireFly's suggestion: 23 chars. Note that read's return type is `a`
-- so you might want to tack on a +1 on ghci etc so `Int` can be derived.
read.reverse.show$1234
BaCon
PRINT REVERSE$(STR$(76543))
Replace 76543 with any number.
Game Maker Language, 44
Replace 12345 with whatever number or variable you want:
n=12345while(n>0){r=r*10+(n mod 10)n div 10}
If you want to prompt for user input, use this 56 character long code instead:
n=get_string('','')while(n>0){r=r*10+(n mod 10)n div 10}
In both pieces of code, the reverse number is stored in r
Language-independent/mathematics
Inspired by Kiril Kirov's answer above. I got curious about the mathematical properties of reversing a number, so I decided to investigate a bit.
Turns out if you plot the difference n - rev(n) for natural numbers n in some base r, you get patterns like this ((n - rev(n)) / (r - 1), for r=10, wrapped at r columns, red denotes negative number):

This sequence could be generated as such (pseudocode):
for i=1 to r:
output 0
for m=0, 1, …
for k=1 to (r-1):
for d=1 to r^m:
for i=0 to (r-1):
output (r-1) * (r+1)^m * (k - i)
If you store these values in a list/array, then n - arr[n] would get you the reversed form of n. Now, to "mathematically golf" this, we'd ideally want a closed-form expression that gives us the n:th value in the sequence, so that we could have a closed-form expression for solving the entire task. Unfortunately, I haven't been able to find such an expression... but it looks like it should be possible. :(
So yeah, not so much a code-golf as a mathematical curiosity, but if there is a closed-form expression of the above sequence it might actually be useful in proper PL golf submissions.
Universal (language agnostic/independent)
If you want to use only numbers (avoid converting the number to string) and don't want to use some specific library (to be universal for any language):
x = 76543 # or whatever is your number
y = 0
while x > 0:
y *= 10
y += ( x %10 )
x /= 10 # int division
This is python, but it could be done in any language, because it's just a math method.
Common Lisp - 60 chars
(first(list(parse-integer(reverse(write-to-string '4279)))))
will get you 9724.
Kinda late but
APL, 3
⍎⌽⍞
If you insists on a function
⍎∘⌽∘⍕
R (35 characters)
> cat(rev(strsplit("12345","")[[1]]))
5 4 3 2 1
With help from my friends in R chat.
In shell scripting :
echo "your number"|rev
Hope this was useful :)
Befunge (3 characters)
Complete runnable program:
N.@
Where N is your number. Rules say "accept a single integer valued argument"; In Befunge you can only enter integers from 0 to 9.
Ruby (14)
x = 13456
x.to_s.reverse
PHP, 9 chars
(int)strrev(123);
To do it short where N is a constant:
strrev(N)
PHP, 37
<?function f($n){return strrev($n);}
C, 44
x;m(n){for(;n;x=x*10+n%10,n/=10);return x;}
APL (3)
⍎⌽⍕
Usage:
⍎⌽⍕12345 => 54321
ruby (26)
The existing ruby answer wasn't a function/lambda, so here goes:
f=->i{i.to_s.reverse.to_i}
k3, 4 chars
0$|$
0$|$76543 \-> gives 34567
See also the K4 solution here. The only difference (saving two chars) is that 0$ converts to int.
in Q (20 characters)
f:{"I"$reverse -3!x}
Sample Usage:
q)f 89478237
73287498
Use the k version of reverse (wrapped in parentheses) to make it shorter
{"I"$(|:) -3!x}
also just define it as a lambda to take 2 chars off for a total of 15
Bash (15)
read a;rev<<<$a
As a number of other entries do, '01234' becomes '43210' and '2340' becomes '0432'; i.e. in Python terms it does print reverse(raw_input()). If behaviour like print int(reverse(str(int(raw_input())))) is expected, it is a bit longer:
Takes care of trailing zeroes (36)
read a;sed s.^0*..\;s.0*$..<<<$a|rev
Golfscript, 5 chars
`-1%~
This takes an argument on the stack and leaves the result on the stack. I'm exploiting the "subprogram" option in the spec: if you insist on a function, that's four chars more leaving it on the stack:
{`-1%~}:r
HTML 21 7 chars (1 char if I'm cheeky...)
‮n
replace n with your number
Scheme, 75 characters
(string->number(list->string(reverse(string->list(number->string 76543)))))
Clojure (42 chars)
#(->> % str reverse(apply str)read-string)
Example usage:
(#(->> % str reverse(apply str)read-string) 98321)
returns 12389
Perl, 11 chars
The p flag is needed for this to work, included in the count.
Usage:
$ echo 76543 | perl -pE '$_=reverse'
Groovy
r={"$it".reverse() as BigDecimal}
assert r(1234) == 4321
assert r(345678987654567898765) == 567898765456789876543
assert r(345346457.24654654) == 45645642.754643543
Perl (19 chars)
Simple:
$x=reverse (98765);
k4 - 6 characters
"I"$|$
Examples:
"I"$|$76543
34567
"I"$|$98765
56789
Explanation from right to left: ("I"$ = cast to integer)(| = reverse)($ = convert to string)
Actionscript
43 characters. num as the parameter to the function:
num.toString().split('').reverse().join('')
This one ACTUALLY takes an input, unlike some of the rest:
print`input()`[::-1]
Python btw.
Vim
17 chars:se ri<CR>C<C-R>"
Lua
Numbers and strings are interchangeable, so this is trivial
string.reverse(12345)
Python 3+
Function form: 28 characters
r=lambda i:int(str(i)[::-1])
(Sub)program form: 25 characters
print(input()[::-1])
I consider some of the other Python examples to be cheating, or at least cheap, due to using hardcoded input and/or not fully satisfying the requirements.
Perl 6
+$n.flip
or:
$n.flip
for dynamically typed code.
Numbers got string methods due to language design.
Python
int(str(76543)[::-1])
EDIT:
Shorter solution as suggested by @gnibbler:
int(`76543`[::-1])
or, if above is unclear:
x=76543
int(`x`[::-1])
Scala - 33 Chars
def r(a:Int)=(a+"").reverse.toInt
C#
int reversed = Convert.ToInt32(String.Join<char>(null, 76543.ToString().Reverse()));
Haskell, 28 24 characters
f=read.reverse.show.(+0)
J - 6 characters + variable
".|.":y
Where y is your value.
Depends on what you mean by short (javascript):
alert(String(123).split('').reverse().join('')),
You could do the following in Java. Note that this converts to String and back and is not a mathematical solution.
public class test {
public static int reverseInt(int i) {
return Integer.valueOf((new StringBuffer(String.valueOf(i))).reverse().toString());
}
public static void main(String[] args) {
int i = 1234;
System.out.println("reverse("+i+") -> " + reverseInt(i));
}
}
this is AS3 code, so you may need to make slight changes
function reverseData ( inData:String ):* {
var ar1 = inData.split(''); //Takes string value, split each digit into an array
var ar2 = ar1.reverse(); //Inverses the array direction
/* //OR (if not supported) [not AS3]
var ar2 = new Array();
for( var i:int = (ar1.length - 1); i > 0; i++ ) {
ar2.push( ar1[i] );
}
*/
var result = ar2.join();
return result;
}
It should work for a string equavalent (that should be easy to typecast)
For a javascript example...
function flip( inData ) {
return ( parseInt( (inData + '').split('').reverse().join('') ) );
}
alert( flip(123) );
It is possible to convert a number a string, then reverse the string and then convert that string back to number. This kind of feature is probably available in all language. If you are looking for a more mathematical method then this might help:
int n = 76543;
int r = 0;
while (n > 0) {
r *= 10;
r += n % 10;
n /= 10;
}