# 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

• @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

# C# - 11 bytes

(a,b)=>a*b;

Anonymous lambda.

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

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

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

## 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 '17 at 19:14

# Math++, 3 bytes

?*?

(Why is there a 30 character minimum?)

• To avoid spam... – dkudriavtsev Jan 16 '17 at 5:36
• @Mendeleev: I bet almost all existing spam existing has more than 30 chars, so the limitation is pointless and ineffective . – sergiol Aug 1 '17 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.

(*)

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

# 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_}

{*}

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 – Muhammad Salman Apr 14 '18 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/… – Martin Ender Apr 17 '18 at 11:40
• @MartinEnder : Oh yeah, nice idea thanks – user79855 Apr 17 '18 at 14:08

# BitCycle, 33 bytes

>>\/v
?+ \/
? +~!/
A + =
~BC^

Try it online!

This takes input via command line args, with the -U flag to convert it into signed unary.

This only uses one pair of collectors in the main loop by representing the first number with unary 0s and the other number with 1s, allowing us to store both in the same collector.

### Explanation:

No fancy gifs sorry. This also assumes you have at least a passing familiarity with BitCycle.

?+       Get both numbers as signed unary from the ? sources
? +      Sends 0s upwards (0s signify that the number is negative), and 1s downwards
>>\/v  Given either 0, 1 or 2 zeroes
+ \/
+ !/  Push a 0 (signifying negative) if there is exactly one 0
+      Push the first number as unary 1s to collector B
+     Push the second number as unary 0s to collector A
B +    Then push the first number into collector A
~AC   Then push both into collector C
/    If the first bit out of collector C is 0
+ =    Turn the = into { and discard the first bit
AC^    All the 0s go back to collector A, and the 1s go up
+~!     Duplicate and print all the 1s
+      Push the duplicates back into A
~A

This loops until it runs out of 0s in the collector, then the = turns into a } and discards the rest of the 1 bits.

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

## OCaml, 4 bytes

( *)

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

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

# dc, 3

?*p

Try it online (wrapped in a shell script to run all given tests).

# Befunge-93, 5 bytes

&&*.@

Try it online!

&& gets two integers and pushes them to the stack; * multiplies them (duh); . prints the numeric value of the top of the stack, and @ ends the program.

# Emacs Lisp, 3 bytes

This is just a function.

(*)

Usage:

(* num1 num2)

isi*

FsF*

# Noodel, noncompeting 1 byte

Did not have a full complete version of Noodel until after this challenge, and did not even have multiplication with a single character until recently.

×

Try it:)

### How it works

# Input is implicitly pushed onto the stack with the first element (a) pushed first and the second last (b) making it the top.
× # Pops off two items producing => (b * a) and pushes on the result.
# The top of the stack is popped off at the end of the program and pushed to stdout.

<div id="noodel" code="×" input="2,-4" cols="5" rows="5"></div>

<script src="https://tkellehe.github.io/noodel/noodel-latest.js"></script>
<script src="https://tkellehe.github.io/noodel/ppcg.min.js"></script>

• @FlipTack, thank you. That should have fixed it. – tkellehe Jan 22 '17 at 14:13

# Swift 3, 40 bytes

func f(_ a:Int,_ b:Int)->Int{return a*b}

Takes two parameter, return the two Ints multiplied. Called like this:

f(2, 4)
• You can also use a closure {$0*$1} and use it like this let res = {$0 *$1} (2,4) //8 – Rodrigo Ruiz Murguía Feb 19 '17 at 0:41

# C (gcc), 98 bytes

m(i,j){i=(i&j)<0?m(O(~i,1),-j):i?O(m(i>>1,O(j,j)),i&1?j:0):0;}O(i,j){i^=j,j&=i^j;i=j?O(i,j<<1):i;}

I don't think I have much of a chance against the other entries.

Try it online!

• Suggest replacing i>>1 with i/2 :) – ceilingcat Jul 27 '17 at 23:12

# Taxi, 442 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:w 1 l 1 r.Pickup a passenger going to Multiplication Station.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:n 1 r 2 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 r 1 l.Pickup a passenger going to Post Office.Go to Post Office:e 1 l 1 r.

Formatted with line breaks for legibility:

Go to Post Office:w 1 l 1 r 1 l.
Pickup a passenger going to The Babelfishery.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:w 1 l 1 r.
Pickup a passenger going to Multiplication Station.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station:n 1 r 2 l.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:s 1 r 1 l.
Pickup a passenger going to Post Office.
Go to Post Office:e 1 l 1 r.
• I take pride in being last place on this challenge. – Engineer Toast Sep 13 '17 at 16:39

# ZX80 BASIC ~39 bytes

1 INPUT A
2 INPUT B
3 PRINT A;"X";B;"=";A*B

This program is so simple that it will very likely work on all variants on 8-bit BASIC. However, on the ZX80 (4K ROM) you are limited to integer maths with a signed 16-bit range; so if your answer is >32767 then it will error and you will not see a result.

To remedy this, install a new ROM onto your Sinclair ZX80 to allow 24-bit floating point maths, or upgrade to a Sinclair ZX81.

Save bytes by refactoring line 3 to:

3 PRINT A*B

## Alice, 7 6 bytes

*/
o@i

Try it online!

Follows the same pattern as my solution for addition.