# Multiply two numbers

Input: Two decimal integers. These can be given to the code in standard input, as arguments to the program or function, or as a list.

Output: Their product, as a decimal integer. For example, the input 5 16 would lead to the output 80.

Restrictions: No standard loopholes please. This is , answer in lowest amount of bytes wins.

Notes: Layout stolen from my earlier challenge, Add two numbers.

Test cases:

1 2   -> 2
4 5   -> 20
7 9   -> 63
-2 8  -> -16
8 -9  -> -72
-8 -9 -> 72
0 8   -> 0
0 -8  -> 0
8 0   -> 0
-8 0  -> 0
0 0   -> 0


Or as CSV:

a,b,c
1,2,2
4,5,20
7,9,63
-2,8,-16
8,-9,-72
-8,-9,72
0,8,0
0,-8,0
8,0,0
-8,0,0
0,0,0


var QUESTION_ID=106182,OVERRIDE_USER=8478;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>

• @FlipTack That's assuming addition and multiplication are as easy in any language, which I don't know if it's actually true. – Fatalize Jan 9 '17 at 8:13
• I don't think it's fair to allow the "add two numbers" challenge but close this one. Even though it's very trivial in most programming languages, it's still a valid challenge. If this is too broad, then the "add two numbers" challenge must also be too broad. – Mego Jan 9 '17 at 8:42
• Anyone is free to downvote trivial challenges if they don't like them, but this is a perfectly valid and on-topic challenge and it's nowhere near "too broad" (if anything, you might call a trivial challenge too narrow). I'm reopening this. That said, if anyone feels that trivial challenges insult their intelligence, I encourage them to seek out languages that make the task less trivial. – Martin Ender Jan 9 '17 at 9:19
• Uo next: Subtract two numbers! – steenbergh Jan 9 '17 at 12:57
• @wat Leaving no barrel-bottom unscraped, eh? – Gareth Jan 10 '17 at 12:39

# Mathematica, 4 bytes

1##&


Example usage: 1##&[7,9] returns 63. Indeed, this same function multplies any number of arguments of any type together.

As Mathematica codegolfers know, this works because ## refers to the entire sequence of arguments to a function, and concatenation in Mathematica (often) represents multiplication; so 1## refers to (1 times) the product of all the arguments of the function. The & is just short for the Function command that defines a pure (unnamed) function.

Inside other code, the common symbol * does act as multiplication. So does a space, so that 7 9 is interpreted as 7*9 (indeed, the current REPL version of Mathematica actually displays such spaces as multiplication signs!). Even better, though, if Mathematica can tell where one token starts and another ends, then no bytes at all are needed for a multiplication operator: 5y is automatically interpreted as 5*y, and 3.14Log[9] as 3.14*Log[9].

• What makes ##& invalid? – Lynn Aug 12 '18 at 12:44
• ##& returns its list of arguments as a 'Sequence' object—suitable for plugging into other functions that take multiple arguments. In this context, ##& doesn't do anything to its list of arguments; we want that list to be multiplied together. – Greg Martin Aug 12 '18 at 17:31

## Labyrinth, 5 bytes

??*!@


Try it online!

Input format can be almost anything (this simply multiplies the first two decimal integers it finds in the input).

# Brachylog V1, 05AB1E, J, K, Underload, MATL, Forth, PigeonScript, Stacked, Implicit, Jolf, Clojure, Braingolf, 8th, Common Lisp, Julia, Pyt, Appleseed, Stax, Reality, 1 byte

*


You may edit this answer to add other languages for which * is a valid answer.

• Polygot, 05AB1E and like 5 other languages. – Magic Octopus Urn Jan 9 '17 at 17:41
• I edited Underload into this. It's possibly the most interesting of these, because Underload does not have a 1 byte method of doing subtraction, division, or addition. – user62131 Jan 12 '17 at 15:13
• Here's another one: codegolf.stackexchange.com/a/106187/62257 – Baaing Cow Feb 22 '17 at 0:23
• This isn't valid in Pyth. Pyth doesn't take implicit input like this. – isaacg Dec 9 '17 at 2:27
• Added Julia, eg *(5,16) – gggg Jan 16 '18 at 23:36

# PigeonScript, 1 byte

*


Explanation:
* looks to the stack to see if there is anything there. If not, it prompts for input and multiplies the inputs together

• This should be added here instead – mbomb007 Feb 17 '17 at 19:15

## JavaScript (ES6), 9 bytes

ES6 has a dedicated function for 32-bit integers, faster than the more generic * operator.

Math.imul


Incidentally, this is just as long as:

a=>b=>a*b

• Awesome, now I know Math.imul, thank you ! – chau giang Apr 1 at 4:55

# Dyalog APL, 1 byte

× takes one number on the left, and one on the right

× ... or even multiple numbers on the left or on the right or on both sides

×/ multiplies all numbers in a list

×/¨ multiplies the pairs in a given list

×/∊ mulitplies all numbers in an array

This applies to all arithmetic functions, arrays of all sizes and ranks, and numbers of all datatypes.

# C (GCC), 13 bytes

Doesn't work on all implementations, but that's OK.

f(a,b){a*=b;}


Try it on TIO!

• Wait, is this supposed to somehow return a? I don't get it... – Erik the Outgolfer Jan 9 '17 at 15:23
• An explanation to how this works would be helpful. (a is a local stack variable to f() - why is its value returned?). +1, btw - very clever abuse of the ABI. – Digital Trauma Jan 9 '17 at 17:29
• @EriktheOutgolfer The return keyword simply places the redult of the its argument in the EAX register. In this case, the generated executable does the computation of a*b in that register, so return doesn't do anything. – Dennis Jan 9 '17 at 18:03
• Hey, that was my trick! codegolf.stackexchange.com/a/106067/18535 :-) – G B Jan 10 '17 at 13:03
• So happy to see C at the top for once! You can actually shave off about 9 bytes by simply replacing the f(a,b){a*=b;} part with 1##& and then just changing your language to Mathematica. – Albert Renshaw Feb 8 '17 at 23:40

## QBIC, 13 bytes

::[a|p=p+b}?p


Adapted my code from 'add two numbers'. This starts a loop and adds 'y' to itself 'x' times.

But seriously, a 6-byte solution is ::?a*b. : gets a cmd line parameter and class it 'a', the next : does the same for 'b', 'cause 'a' is already taken. * multiplies, and ? prints the result. This is virtually identical to this answer, only the operator is different.

Since some time now, QBIC can in-line the 'get var from cmd line'-command, and the above would be ?:*:, at 4 bytes.

# MATLAB, 5 4 bytes

@dot


dot takes the dot product of two vectors of equal length. If we feed it with two scalars, it will simply multiply the two numbers.

prod takes the product of the values in all rows of each column of a matrix. If the matrix is one-dimensional (i.e. a vector), then it acts along the non-singleton dimension, taking the product of all elements in the vector.

dot is one byte shorter than prod which is one byte shorter than the even more obvious builtin times.

Call it as such:

@dot
ans(3,4)
ans =
12


# Scratch, 1 byte

Usage: Place numbers in both sides of * sign

Note: Since Scratch is a visual language I could not figure out how many bytes it consumes until @mbomb007 noted me about a method for counting scratch bytes

## C#, 10 bytes

a=>b=>a*b;


It's just a simply multiplication.

• You beat me to it! – TheLethalCoder Jan 9 '17 at 14:27
• How does the => => work? I'd expect (a,b)=>a*b; – Carra Jan 11 '17 at 10:54
• @Carra It works, that this lambda expression returns a delegate, which returns the result, so you call it this way, if you call this lambda f:f(a)(b). – Horváth Dávid Jan 11 '17 at 11:27
• This would be a form of function currying – ThePlasmaRailgun Apr 1 at 20:33

# Jelly, 1 byte

×


Try it online!

Obligatory Jelly submission.

# 05AB1E, 1 byte

*


Try it online!

• @downvoter: This is a built-in answer, and the convention is not to upvote them too much. But you should not downvote a perfectly valid answer either. – Erik the Outgolfer Feb 4 '17 at 8:12
• Upvoted it to make up for whoever downvoted – Metoniem Feb 21 '17 at 9:21
• @Metoniem I think that upvoting just because you feel sorry isn't recommended though. – Erik the Outgolfer Feb 21 '17 at 11:40

# Python 2, 14 bytes

lambda a,b:a*b


Try it online!

I feel like this is too much shorter than from operator import*;mul.

• Why the downvote? This is a perfectly valid submission. – Erik the Outgolfer Jan 9 '17 at 17:57
• int.__mul__ would also be valid, but that's already posted. – Yytsi Jan 10 '17 at 6:29
• @TuukkaX Duplicate answers are allowed, but I don't want to dupe an answer I have seen in the past purposefully. – Erik the Outgolfer Jan 10 '17 at 9:43

## Pyth, 4 bytes

M*GH


Defines a function named g, can call it in this way - gn1 n2, where n1 and n2 are the numbers.

Try it here!

Previous solutions using 5 bytes.

*.).Q and *hQeQ.

# Java 8, 10 9 bytes

a->b->a*b


Try it here.

# Java 7, 31 bytes

int c(int a,int b){return a*b;}


Try it here.

As full program (99 90 bytes):

interface M{static void main(String[]a){System.out.print(new Long(a[0])*new Long(a[1]));}}


Try it here.

• There's a typo in you full program, should be * instaed of +. – corvus_192 Jan 9 '17 at 14:18
• You don't need parenthesis around a,b in the lambda expression. – FlipTack Jan 9 '17 at 17:07

## Hexagony, 9 bytes

?{?/*!@'/


Try it online!

This is actually fairly straightforward. Here is the unfolded version:

  ? { ?
/ * ! @
' / . . .
. . . .
. . .


The / just redirect the control flow to the second line to save bytes on the third. That reduces the code to this linear program:

?{?'*!@


This linear code on its own would actually be a valid solution if the input was limited to strictly positive numbers, but due to the possibility of non-positive results, this isn't guaranteed to terminate.

The program makes use of three memory edges in a Y-shape:

A   B
\ /
|
C


The memory pointer starts on edge A pointing towards the centre.

?   Read first input into edge A.
{   Move forward to edge B.
?   Read second input into edge B.
'   Move backward to edge C.
*   Multiply edges A and B and store the result in C.
!   Print the result.
@   Terminate the program.


I ran a brute force search for 7-byte solutions (i.e. those that fit into side-length 2), and if I didn't make a mistake (or there's a busy-beaver-y solution that takes a long time to complete, which I doubt) then a 7-byte solution doesn't exist. There might be an 8-byte solution (e.g. by reusing the ? or using only one redirection command instead of two /), but that's beyond what my brute force search can do, and I haven't found one by hand yet.

## Pyth, 2 bytes

*E


Try it here!

Pyth's automatic evaluation gets in the way here. To get around it, I'm using explicit evaluation for one of the arguments

• Wow, that's nice. This will be handy in future. – Gurupad Mamadapur Jan 9 '17 at 12:34

# Python 3, 11 bytes

int.__mul__


Try it online!

Also works for integers under 2**32 in Python 2.

# Groovy, 10 bytes

{x,y->x*y}


This is an unnamed closure.

Try it here!

## OCaml, 4 bytes

( *)


The space after the first parenthesis is needed because (* starts a comment in OCaml.

Try it online!

# Japt, 1 byte

×


Here is a non-1 byte solution

U*V


Run it here

• × was added before this challenge was posted (at 13:37 UTC on 7 Jan 2016), so this answer is actually competing – ETHproductions Jan 10 '17 at 0:54
• Did I say 2016? Gosh, I really need to get used to this whole "2017" thing :P – ETHproductions Jan 10 '17 at 2:21

(*)


Call it as a function: (*) a b

or as an operator: a * b .

• Our standard is to require (*) so that the expression evaluates to a function and can be stored. – xnor Jan 9 '17 at 16:59
• @xnor I'm not sure, but I think if there is a built-in command, then it seems that just writing that down is enough. (At least when looking at other challenges/submissions.) – flawr Jan 10 '17 at 13:23
• @flawr It's difficult to score this, to assign it to a new name, you need to add parenthesis, but it can be called as a*b without. – corvus_192 Jan 10 '17 at 13:32
• According to the definition I gave on Meta, which seems well-received at the moment, this sort of function definition must be an expression that evaluates to a value of function type. * is not an expression in Haskell. (*) is. – user62131 Jan 11 '17 at 21:22
• Proton Polyglot :P – HyperNeutrino Oct 6 '17 at 20:49

# Scala, 2 bytes

_*


This is a curried lambda / anonymous function. To use it, assign it to a variable:

val f:(Int=>Int=>Int)=_*
f(7)(9)                   //returns 63


## How it works:

In Scala, the underscore can be used as a shorthand for the arguments.

_*_, for example, is syntactic sugar for (a,b)=>a*b.

Removing the second underscore is treating the method * of the first argument as a function, results in a curried function that multiplies its arguments.

# Brain-Flak, 56 bytes

([({}<([({})<>])<>>)<>]){({}[()]<(({})<({}{})>)>)<>}{}{}


This must be run as a full program as it is not stack clean and the inputs must be the only elements in either stack.

Try it online!

Explanation: (call the inputs x and y)

Part 1:

([({}<([({})<>])<>>)<>])

([                    ]) # Push negative x on top of:
([      ])         # negative y. After...
({}<            >)     # pushing x and...
({})             # y...
<>  <>  <>   # on the other stack (and come back)


At this point we have [x,y] on one stack and [-x,-y] on the other.

Part 2:

{({}[()]<(({})<({}{})>)>)<>}{}{}
{                          }     # Loop until x (or -x) is 0
({}[()]<              >)        # Decrement x
(({})<      >)          # Hold onto y
({}{})            # Add y and the number under it (initially 0)
<>      # Switch stacks
{}{} # Pop x and y leaving the sum

• Wow! Definitely the most impressive answer so far – DJMcMayhem Jan 9 '17 at 17:44
• @DJMcMayhem And (slightly modified) it beats the one on the wiki by 18 bytes – Riley Jan 9 '17 at 18:23
• Do you have write access to the brain-flak wiki? I'd love to upload a shorter version. – DJMcMayhem Jan 9 '17 at 18:24
• @DJMcMayhem I do not have access. I posted the shorter one in the Brain-Flak chatroom if you want to take a look, and upload it. – Riley Jan 9 '17 at 18:26
• I know its been a while but you have some competition ;) – Sriotchilism O'Zaic Sep 13 '17 at 16:07

## Clojure, 1 byte

*


:P As a bonus this works on any number of arguments:

[(*)
(* 2)
(* 2 3)
(* 2 3 4)
(* 2 3 4 5)] => [1 2 6 24 120]


Interestingly you can easily get its source code:

(source *)
(defn *
"Returns the product of nums. (*) returns 1. Does not auto-promote
{:inline (nary-inline 'multiply 'unchecked_multiply)
:inline-arities >1?
([] 1)
([x] (cast Number x))
([x y] (. clojure.lang.Numbers (multiply x y)))
([x y & more]
(reduce1 * (* x y) more)))


# TI-Basic, 2 bytes

Very straightforward.

prod(Ans

• Ans is not an allowed I/O method. – Mego Jan 9 '17 at 18:47
• According to who? That link shows seven votes – Timtech Jan 10 '17 at 1:36
• @Timtech it wasn't at the time of the comment but it was posted in chat so just became valid – Blue Jan 10 '17 at 7:45
• Alright, thanks for the tip @muddyfish – Timtech Jan 10 '17 at 23:08

# FOG, 3 bytes

^^*


Takes input twice and multiplies. I can also just use * and call it a function, but for the sake of uniqueness I decided not to.

# CJam, 3 bytes

{*}


Try it online!

# Pushy, 2 bytes

*#


Try it online!

As you might have guessed, * is the multiplication operator, and # prints the result.

• You might be able to remove the the last byte? I don't think output means it has to be printend :o – Metoniem Feb 21 '17 at 9:23