# Given an int input n, print out n*reversed(n)

Given an integer n, print out n * reversed(n)

reversed(n) is the number you get when you reverse the digits of n.

reverse(512) = 215

reverse(1) = 1

reverse(101) = 101

>>>>>>>>


func(5) = 5*5 = 25

func(12) = 12*21 = 252

func(11) = 11*11 = 121

func(659) = 659*956 = 630004


Shortest code wins!

var QUESTION_ID=144816,OVERRIDE_USER=71625;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

• What is the reverse of 100?
– tsh
Oct 10, 2017 at 2:33
• 001, but in any case, its fine if you have additional zeros in front Oct 10, 2017 at 2:41
• Why downvote? Because this challenge is too trivial (compare to some other challenges, it is not!) or because it is badly-formatted / unclear? Oct 10, 2017 at 10:31
• @user202729 I downvoted because I didn't see or anticipate a lot of variety or depth to answers. This question is boring, I tried it. Part of the reason it is boring is because its trivial, which I think is a perfectly just cause to downvote a question on its own. Oct 10, 2017 at 19:46
• What if the input is negative? Aug 29, 2022 at 17:50

# 05AB1E, 2 bytes

R*


Try it online!

In 05AB1E, integers and strings are treated as equivalent types, so reversal (R) converts to string and reverses, whilst multiplication (*) treats the reverse and the input as integers.

• Â* is valid too :D. Oct 10, 2017 at 19:45

# JavaScript (SpiderMonkey), 453533 28 bytes

n=>n*[...n].reverse().join


Try it online!

• Welcome to PPCG! You don't have to count f=; anonymous functions are allowed by default. Oct 10, 2017 at 3:11
• Switch out (n+"").split("") for [...(n+"")] to save a few bytes. You don't need the unary plus, and the parentheses around the reversed string are extraneous. All in all, that saves you 10 bytes. Oct 10, 2017 at 4:51
• You can replace .join("") with .join to save 2 bytes. Oct 10, 2017 at 5:25
• Save 5 bytes by taking input as a string: tio.run/##BcFRCoAgDADQu/ilQYP6t4tEoNgMzTZxIXR6ey/… Oct 10, 2017 at 6:37

# Jelly, 3 bytes

×ṚḌ


I'm new to Jelly, so please let me know if there is a way to do this in 1 or 2 bytes!

Try it online!

### Explanation

×ṚḌ    (Input: 12)

Ṛ     Reversed decimal digits (Stack: [2, 1])
×      Multiply by input       (Stack: [24, 12])
Ḍ    Convert to decimal      (Stack: 252)
Implicit print

• Actually ṚḌ× would be a better version. Of course this would still work, due to how bases work. Oct 10, 2017 at 11:33
• @EriktheOutgolfer I originally wrote that first, but moved the × for fanciness ;) Oct 10, 2017 at 12:35

# Ruby, 25 24 bytes

->n{n*eval(n.digits*'')}


Integer#digits returns a list of reversed digits, so further reversing is not necessary.

Thanks to @benj2240 for golfing a byte!

• You can save a byte with eval Feb 20, 2018 at 20:42
• @benj2240 thanks! :) Feb 21, 2018 at 18:01
• The eval route doesn't actually work, as it will cause the program to fail on 900 with error Invalid octal digit (SyntaxError) instead of returning the expected 8100... Feb 7, 2020 at 1:14

# Perl 5, 11 + 1 (-p) = 12 bytes

$_*=reverse  Try it online! # ARBLE, 12 bytes Takes input as an int. a*reverse(a)  Try it online! • Thats why I said integer input ;), does int still work? Oct 10, 2017 at 2:23 • @KSplitX Oh, I didn't notice that. I'd think that restriction a bit unnecessary, but it's up to you. Oct 10, 2017 at 2:24 • @KSplitX Fixed. Oct 10, 2017 at 2:25 • Should use n * reverse(n) so it matches the question's specification word for word (Given an integer n, print out n * reversed(n)) – Okx Oct 11, 2017 at 15:39 # Python 3, 35 28 bytes lambda m:m*int(str(m)[::-1])  Try it online! Saved 7 bytes by fixing a bug pointed out by Dennis. • Nah its clear, you convert to string, the [::-1] reverses it, then we eval xD Oct 10, 2017 at 3:48 • This doesn't work for input 80, as 08 is an invalid (octal) literal. Oct 10, 2017 at 4:47 • Saved 7 bytes by fixing a bug Don't you just love it when that happens... Oct 10, 2017 at 18:25 # J, 7 bytes *|.&.":  Try it online! Couldn't think of a shorter way, though I feel like this is pretty elegant. ### Explanation *|.&.": &.": Convert to string, apply next function, then undo conversion |. Reverse * Multiply by input  # Haskell, 27 23 bytes 4 bytes saved thanks to Lynn and Laikoni (*)=<<read.reverse.show  Try it online! • 23 bytes without import: Try it online! Oct 10, 2017 at 6:07 • With Control.Monad it is also 23 bytes: ap(*)$read.reverse.show. Oct 10, 2017 at 6:09
• No need for Control.Monad. (*)=<<read.reverse.show works fine.
– Lynn
Oct 10, 2017 at 13:22
• @Lynn Thanks! I feel like I should have seen that. Oct 10, 2017 at 13:26

# Perl 6, 10 bytes

{$_*.flip}  Try it ## Expanded { # bare block lambda with implicit parameter$_
$_ * .flip # implicit method call on$_
}


# bash, 52 48 bytes

for((i=n=$1;r=r*10+i%10*n,i/=10;));{ :;};echo$r

• You can replace do...done with curly braces to save some bytes: Try it online!. This is from the tips for Bash post. Oct 10, 2017 at 17:28
• thank-you, I'm glad to learn it, I had never seen this syntax, updating my post Oct 10, 2017 at 19:42
• Isn’t that shellshock? Feb 24, 2018 at 23:45

# C# .NET, 55 bytes

n=>{int i=n,j=0;for(;i>0;i/=10)j=j*10+i%10;return n*j;}


Explanation:

Try it here.

n=>{           // Method with integer as both parameter and return-type
int i=n,     //  Integer i (starting at the input)
j=0;     //  Integer j (starting at 0)
for(;i>0;    //  Loop as long as i is not 0
i/=10)   //    After every iteration: Remove the last digit of i
j=j*10     //   Add a trailing zero to j,
+i%10;   //   and then sum this new j with the last digit of i
//  End of loop (implicit / single-line body)
return n*j;  //  Return the input multiplied with j
}              // End of method


# Ohm v2, 2 bytes

œΠ


Try it online!

Explanation:

œΠ   Main wire, arguments: n

œ    Pushes [n, n.reverse]
Π   Multiplies that array together
Implicit output

• stays on the phone for 4 months :P Feb 22, 2018 at 15:37
• You must be put on hold for a long time for there to be no explanation Apr 25, 2019 at 2:34
• @MilkyWay90 We've all been stuck on hold for a year and a half before, right? Apr 26, 2019 at 14:27
• @NickClifford Not all of us have Apr 26, 2019 at 20:11

## Batch, 87 bytes

@set s=%1
@set r=
:l
@set/ar=r*10+s%%10,s/=10
@if %s% gtr 0 goto l
@cmd/cset/a%1*r


Need to take the arithmetic route here as string reversal fails for some numbers such as 80.

# C (gcc), 49 bytes

i,a;f(n){for(i=0,a=n;a>0;a/=10)i=i*10+a%10;n*=i;}


Try it online!

• Suggest replacing a>0 with just a Oct 10, 2017 at 17:53

# LISP, 9164 bytes

(defun R (N)(defvar M (write-to-string N)) (parse-integer (reverse M))) (write (* x (R x)))

(defun R(N)(write(* N(parse-integer(reverse(write-to-string N))))))


Where x N is your integer you want to work with, of course.

I'm pretty new to programming, but I've found that trying these Code Golf problems has been nice practice. Is there something I'm missing that could help with this?

EDIT: Thanks to some tips from ceilingcat, I was able to shave off a few bytes. Old program preserved in strikethrough for reference.

• Welcome to Code Golf! You can eliminate some whitespace and maybe ditch a variable assignment. Also, by convention you may be able to just return the output instead of (write ...) Oct 10, 2017 at 17:56
• You can save a byte using a lambda instead of defun. Also, read tips for golfing in lisp Oct 10, 2017 at 19:59

# Bash + GNU utilities, 18

bc<<<$1*rev<<<$1


# Batch, 150125 121 bytes (+ 5 bytes? cmd/q)

set l=%1
set n=0
set r=
:L
call set t=%%l:~%n%,1%%%
set/an+=1
if [%t%] neq [] set r=%t%%r%&goto L
set/ar=%r%*%l%
echo %r%


Saved 25 bytes thanks to user202729!

Saved 4 bytes thanks to Matheus Avellar!

• 97 bytes Oct 10, 2017 at 13:29
• Or, 87 bytes Oct 10, 2017 at 13:40
• Isn't this Batch, not Bash? The TIO for bash does not work for this. Oct 10, 2017 at 17:26
• Yes, yes it is; sorry about that Oct 10, 2017 at 17:44
• You can inline that if to be at 121 bytes: if [%t%] neq [] set r=%t%%r%&goto L. However, I think you gotta include 1 byte for the /Q flag passed tocmd so it runs with implicit @echo off Oct 11, 2017 at 1:48

# ><>, 41 39 Bytes

:&>:a%:}-\
/~\?)0:,a/
>l1-?\&*n;
\ +*a/


## How it works:

:&


Assume input has been pushed to the stack (https://codegolf.meta.stackexchange.com/a/8493/76025). Duplicate it and store a copy in the register.

   >:a%:}-\
\?)0:,a/


Converts this to its individual digits, leaving them on the stack.

/~
>l1-?\
\ +*a/


The top value will always be a zero due to the number-to-digit conversion process; drop it from the stack. Now, while the length is >1, multiply the first item by ten and add it to the item below it. This results in the number reversed.

      &*n;


Multiply the original number by the reverse, print the answer, and stop.

• 33 bytes
– Jo King
Jan 26, 2018 at 12:40

# Vyxal, 2 bytes

Ṙ*


# Julia, 28 bytes

~n=parse(Int,reverse("$n"))n  Attempt This Online! # Leaf Lang, 41 bytes Note: The interpreter does not have a stable release. If I introduce a breaking change I will update this post. import"string.lf"argv:o o reverse o*print  Explained: import "string.lf" - import stdlib's string.lf argv - push all of argv onto stack :o - set o to top value from stack (pops top) o - push o to top of stack reverse - reverse the string o - push o to top of stack * - leaf lang will implicitly convert strings to numbers when applicable print - print top value of stack with no newline  Ungolfed: import "string.lf" argv : num num reverse num * print  # Mathematica, 19 bytes # IntegerReverse@#&  Takes an integer input. # cQuents 0, 8 bytes #|1:A\rA  Try it online! ## Explanation #|1: Output first term in sequence A\rA Each term in the sequence equals: A * \reverse(A)  # Ly, 7 bytes nsSrJl*  Try it online! # Casio-Basic (fx-CP400), 44 bytes ExpToStr n,a StrInv a,a Print n*strToExp(a)  There's no built-in for reversing an integer, but there is one for reversing a string. ExpToStr n,a turns n into a string and stores it in a, then StrInv a,a overwrites a with the reversed version of itself. The last line turns a into a number, and prints n*a. 43 bytes for the code, +1 to input n into the parameters box. # Japt, 2 bytes Takes input as a string, outputs an integer. *w  Try it • Wow, I thought it would have to be 4 bytes at least... I suppose it's only 3 even if taking input as an integer *sw Oct 10, 2017 at 18:26 • @ETHproductions: yeah, that's what I had originally. Oct 10, 2017 at 18:40 # MATLAB/Octave, 33 31 bytes @(n)str2num(flip(int2str(n)))*n  Try it online! Octave/MATLAB anonymous function. This is a pretty naïve approach - converts the integer to a string, flips the string, converts the result back to an integer and multiplies it by the original. • Save 2 bytes by using flip instead of fliplr. # Python 2, 25 bytes lambda n:n*int(n[::-1])  Try it online! # PHP, 23+1 bytes <?=$argn*strrev(\$argn);


Save to file and run as pipe with -nF.