# 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. Jan 9, 2017 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.
– user45941
Jan 9, 2017 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. Jan 9, 2017 at 9:19
• Uo next: Subtract two numbers! Jan 9, 2017 at 12:57
• @wat Leaving no barrel-bottom unscraped, eh? Jan 10, 2017 at 12:39

## 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)))


# C# - 11 bytes

(a,b)=>a*b;


Anonymous lambda.

(*)


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, 2017 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.) Jan 10, 2017 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. Jan 10, 2017 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, 2017 at 21:22
• Proton Polyglot :P Oct 6, 2017 at 20:49

# LOLCODE, 95 bytes

HAI 1.3
I HAS A J
GIMMEH J
I HAS A Q
GIMMEH Q
I HAS A T
T R PRODUKT OF J AN Q
VISIBLE T
KTHXBYE


Try it online!

# In Floop, 70 bytes, pos. only

r+s[r-]r[-n?o?s+]r+s-[@[-r-n+]r[s-o-[r-]r[-@;]]]r[n[-r@+r-@+]r[-s+]]s+


I wrote this in three parts:

• Outer part: r+s[r-]r[-n?o?s+]r+s-[A]r[B]s+ (based off of my add-two-numbers program)
• A: @[-r-n+]r[s-o-[r-]r[-@;]] (copies @1 to n, 1 at a time)
• B: n[-r@+r-@+]r[-s+] (copies n to @1 and adds it to @1, 1 at a time)

This forms a loop, until o- is 0 after A runs. Then, @0 is printed.

Try It Online!

## 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).

# 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. Feb 4, 2017 at 8:12
• Upvoted it to make up for whoever downvoted Feb 21, 2017 at 9:21
• @Metoniem I think that upvoting just because you feel sorry isn't recommended though. Feb 21, 2017 at 11:40

# 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.

# 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 Jan 10, 2017 at 0:54
• Did I say 2016? Gosh, I really need to get used to this whole "2017" thing :P Jan 10, 2017 at 2:21

## C#, 112 63 bytes

Saved 49 bytes, thanks to milk

a=>b=>{int r=0,i=0,k=0<b?1:-1;for(;i!=b;i+=k)r+=a;return r*k;};


I've already posted an answer using the multiplication operator, but here I do it with a loop.

• It's only 63 bytes if you don't use System.Math: a=>b=>{int r=0,i=0,k=0<b?1:-1;for(;i!=b;i+=k)r+=a;return r*k;};
– milk
Jan 10, 2017 at 19:14

# Math++, 3 bytes

?*?


(Why is there a 30 character minimum?)

• To avoid spam... Jan 16, 2017 at 5:36
• @Mendeleev: I bet almost all existing spam existing has more than 30 chars, so the limitation is pointless and ineffective . Aug 1, 2017 at 22:27

~A:set B =@IN
~A:set A =@IN
B:~C:decr B
~C:set C =A
C:incr D
C:decr C
set G =0
B:incr G
C:incr G
~B:print D
~B:exit


Run from m.ldr, or alternatively replace the m in the third line from the end with the name of the file.

# SimpleTemplate, 59 bytes

Since this language doesn't have math (yet), it requires a loop and an increment, to generate the result.

{@setx 0}{@for_ from1 toargv.0}{@incbyargv.1 x}{@/}{@echox}


Ungolfed, with whitespace:

{@set prod 0}
{@for _ from 1 to argv.0}
{@inc by argv.1 prod}
{@/}
{@echo prod}


This answer was written to run on the commit 2166e6bdac44064ec5594d511528d1469ea3feef from 2017-01-07.

On the latest version (commit 9857e4277536555d0b06e8ef9c00ba0c7f23cf6d), one could do like this (50 bytes):

{@for_ from1 toargv.0}{@incbyargv.1 x}{@/}{@echox}


This will show a warning saying that the index 'x' doesn't exist.

With a new update (commit 552216290ec0d8cb9893e08d89601c4d67fcc3d1), the code can be written as (21 bytes):

{@set*_ argv}{@echo_}


# CJam, 3 bytes

{*}


Try it online!

# Cubix, 6 bytes

OI|u*@


This is a slightly different algorithm than the "Add Two Numbers" Cubix solution. It would have been trivial to simply use that solution with a * instead of a + but the flow here is slightly different.

On a cube:

  O
I | u *
@


rather than using the control flow \ or / and then duplicating I to read in the inputs, I opted to use | to pass over the I twice instead, which forced me to use u to get to the output commands.

The commands are:

• I : read in input
• | : reflect IP left-right
• I : read in input
• * : multiply
• u : do a right-hand u-turn (i.e., two right turns)
• O : output as a number. the IP is now facing North, and has a right turn to make, which takes it to
• * : multiply the top two numbers (essentially a no-op)
• @ : terminate program.

Similarly to the addition solution, the cube's symmetry allows the following solution.

  @
I | U *
O


Additionally, replacing | with < or T will work as well.

Try it online!

# x86_64 machine language (Linux), 5 4 bytes

0:       97                      xchg   %edi,%eax
1:       f7 ee                   imul   %esi
3:       c3                      retq


To Try it online!, compile and run the following C program.

#include<stdio.h>

int f(int a,int b){return a*b;}
const char g[]="\x97\xf7\xee\xc3";

int main(){
for( int i = 0; i < 10; i++ ) {
for( int j = 0; j < 10; j++ ) {
printf( "%d %d %d %d\n", i, j, f(i,j), ((int(*)())g)(i,j) );
}
}
}


# JavaScript (Node.js), 39 36 bytes

-3 bytes thanks to @MartinEnder

f=(a,b)=>b?b>0?a+f(a,b-1):-f(a,-b):0


Try it online!

This is me having fun , it won't win any competitions but it uses recursion and doesn't use the * operator.

# Explanation :

f = (a,b) => {
if (b == 0) {
return 0;
}
if (b > 0) {
return(a + f(a , b-1))
}
else {
return(-f(a , -b))
}
};

• Cool. Voted up. Very interesting answer Apr 14, 2018 at 13:20
• You can save three bytes by dropping ==0 and swapping the true and false branches of the outer ternary: tio.run/##Zc7RCoIwGAXg@55il265@P8JhYL6LJtpFOIko9df/… Apr 17, 2018 at 11:40
• @MartinEnder : Oh yeah, nice idea thanks
– user79855
Apr 17, 2018 at 14:08
• also 36 bytes: no addition
– c--
Jul 24, 2022 at 4:44

# √ å ı ¥ ® Ï Ø ¿, 4 bytes

II*o


### Explanation

I    › Take input from the command line, evaluate and push to stack
I   › Take another input
*  › Times the two values together and push to stack
o › Output the first value on the stack


# Intcode, 22 bytes

3,0,3,1,2,0,1,2,4,2,99


Fairly basic. IO boilerplate takes up most of the bytecount, given that the language has multiplication as a builtin (2,0,1,2 would be a valid answer, if snippets were allowed)

# International Phonetic Esoteric Language, 4 bytes (WIP language) (OLD)

ɪɪθo


No TIO interpreter yet, but is runnable by cloning the repository above, and calling python3 main.py "code here".

ɪɪθo
ɪ    ; push int
ɪ   ; push int
θ  ; pop, pop, push a * b
o ; pop, print


# Brainetry, 257 bytes

Golfed version. A more interesting submission follows.

a b c d e f
a b c
a b c d e f
a b
a b c d e f g h
a b c d e
a b c
a b c d e f g h
a b c d e
a b c
a b c d
a b c
a b c d
a b
a b
a b c d e f g h i

a b c d e f g h
a b c d e
a b
a b
a b c d
a b c
a b c
a b c d e f g h i
a
a b c d e f g h i

a b
a b c d e f g


To try this online, follow this repl.it link, paste the code in the btry/replit.btry file and press the green "Run" button.

Ungolfed version:

This is probably my first Brainetry
program that makes
non trivial use of some of
my new
experimental operators that I introduced, expanding the base
brainfuck implementation this is built
upon. We will
see if this made any difference or not.
This program works in a
rather simple way.
Then I have
to nest two loops.
One loop,
two loops.
The two loops will be nested, which is nice.

One loop will have me decrement one of
the user inputs, then I
move left,
(not right)
and successively decrement the
cell I'm at.
I do this
and keep accumulating in another cell. If I loop
with
enough dedication, the final result will be stored someplace.

Legend says,
the correct place is the second cell.

• lol, the ungolfed program is self-explanatory.
– user92069
Jun 14, 2020 at 6:27
• @Memberfor3months yes it is :)
– RGS
Jun 14, 2020 at 9:07

# Foam, 10 bytes

;# ;# * .#


Try it online!

I'm gonna be using this lang for the next few weeks

;# returns an integer from input.

* is...multiplication.

.# is output.

# LOLCODE, 77 bytes

HAI 1
I HAS A a
I HAS A b
GIMMEH a
GIMMEH b
VISIBLE PRODUKT OF a AN b
KTHXBYE


I has a bettr answar cuz I used PRODUKT OF a AN b in da VISIBLE direktly instaed of in an variable !!1!

Try it Online!

• Gramaer misteak on porpose !1!! Feb 2, 2022 at 8:31

# Piet, 10 9 codels

Original image

The 2 white codels on the side can be replaced with any color. Most straightforward solution, literally "read 2 numbers, multiply, output", I really don't know why this answer is more complicated.

Update: saved 1 codel by switching from a 2x5 to a 3x3 grid

• @DLosc Did you forget the 2 white pixels at the bottom? Works fine with gabriellesc.github.io/piet Apr 6, 2022 at 20:19
• Edited the image to clarify Apr 6, 2022 at 20:26
• I'm thinking MasterPiet acts as if the whole PNG is surrounded by black codels Apr 6, 2022 at 22:21
• I apologize, I was wrong about white blocks. The Esolangs article doesn't describe them properly, and I hadn't read the actual spec in a while. My guess now is that perhaps Npiet treats white blocks correctly except that it doesn't do the "exit if infinite loop" part. Apr 7, 2022 at 21:34
• @DLosc Apology accepted :) Apr 7, 2022 at 21:39

# rusty_deque, 2 bytes

Two variants: one for the left of the deque, one for the right of the deque.

*! # left
*~ # right


# 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. Jan 9, 2017 at 17:57
• int.__mul__ would also be valid, but that's already posted. Jan 10, 2017 at 6:29
• @TuukkaX Duplicate answers are allowed, but I don't want to dupe an answer I have seen in the past purposefully. Jan 10, 2017 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.

## OCaml, 4 bytes

( *)


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

Try it online!

# 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.

# 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 Feb 21, 2017 at 9:23