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

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

# C, 56 bytes

main(int i,char**a){printf("%d",atoi(*++a)*atoi(*++a));}


compile

> gcc source.c


usage

> a 8 7


or

> ./a.out 8 7


explanation

main(int i,char**a) {  /* Begin program and accept command-line arguments.    */
printf("%d",        /* First argument prints the (final) decimal result.   */
atoi(*++a)    /* Returns int of program's 2nd command-line argument. */
*             /* Multiplies the values of the 1st & 2nd arguments.   */
atoi(*++a)    /* Returns int of program's 1st command-line argument. */
);
}

• Request: Don't put the byte count far away from the language and use a code block for the explanation. – CalculatorFeline Feb 22 '17 at 22:23
• @CalculatorFeline Why? – veganaiZe Feb 22 '17 at 22:24
• Well, I guess the byte count formatting is okay, but the explanation looks really weird. – CalculatorFeline Feb 22 '17 at 22:25
• That's my style (for the moment)! If you don't like it then please offer me something more than really weird -- Thanks for the feedback. – veganaiZe Feb 22 '17 at 22:29
• Your formatting is all over the place on my phone. Also, I'm sorry, but I just don't get why you'd include an explanation at all if you're not going to make it readable. – Dennis Feb 22 '17 at 23:07

# Lean Mean Bean Machine, 12 bytes

OO
ii
/
*
u


# Whitespace, 4 bytes





In some other languages, you could quote that as "\t \n". Expects two numbers on the stack and leaves the result on the stack.

As a complete program reading from standard input and writing to standard output, it would be 36 bytes:





Try it online!

• I know it's been quite a while, but you can golf 1 byte in the full program. Instead of Push_0, Duplicate, STDIN_as_integer, Retrieve for the second input, you can use Duplicate, Duplicate, STDIN_as_integer, Retrieve. Duplicate (SNS) is one byte shorter than Push_0 (SSSN). It will store the second input on the heap at the address of the first input (so if the first input is 3, it will store the second input (i.e. 5) in the heap at {3:5} instead of {0:5}). – Kevin Cruijssen Mar 13 '18 at 8:57
• Ignore my comment above. Apparently you cannot use an negative integer as heap-address, so it would fail if the first STDIN is negative. – Kevin Cruijssen Mar 13 '18 at 10:31
• @KevinCruijssen Interesting try, though. And I wouldn't be surprised if at least one existing Whitespace interpreter does allow negative heap addresses... – aschepler Mar 13 '18 at 13:04

# Perl 5, 7 bytes

6 bytes of code + 1 for -p flag

$_*=<>  Try it online! # Lua, 32 bytes i=io.read print(i("*n")*i("*n"))  Try it online! Program reads 2 numbers from STDIN and prints the result to STDOUT. "But shouldn't you use io.write?" Well according to the documentation, [...] write uses the current output file, whereas print always uses the standard output. Finally, print automatically applies tostring to its arguments, so it can also show tables, functions, and nil. # Go, 84 bytes package main import."fmt" func main(){a,b:=0,0;Scanf("%d%d",&a,&b);Printf("%d",a*b)}  Try it online! Worth mentioning: • We import the fmt package to the global namespace by using import.. That way, we can refer to Scanf and Printf as global functions. • a and b are assigned to 0, as that is shorter than doing var a,b int. # Go, 28 bytes func(a,b int)int{return a*b}  Try it online! # Ruby, 10 bytes :*.to_proc  Try it online! I really thought this might be shorter... • ->a,b{a*b} works too lol – anna328p Oct 7 '17 at 20:11 • Yeah, I thought I'd try something different. :P – totallyhuman Oct 8 '17 at 3:09 # C#, 27 bytes delegate(a,b){return a*b;};  • Welcome to PPCG! You can make this shorter with (a,b)=>a*b, or use currying to get a=>b=>a*b. However, someone has already posted this solution. – Pavel Nov 2 '17 at 1:57 # Aceto, 6 bytes riri*p  ri grabs input and converts to integer * multiplies them p prints it Try it online! • Also works in CJam, interestingly. – Esolanging Fruit Mar 11 '18 at 23:35 ## 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. # Forked, 5 bytes $$*%&  Try it online! • $$ - read two integers • * - multiply top two stack values • % - print top of stack as integer • & - "terminate", prevent IP from wrapping # FRACTRAN, 22 bytes 78/55 5/3 1/5 11/2 5/7  Take 7^a*11^b as input, return 11^b*13^(a*b). Try it online! This is a golfed version of the wikipedia example. I'm not sure whether or not extra bytes should be added for input encoding and output decoding. # Flobnar, 4 bytes & *@  Try it online! • :| this is too golfy – ASCII-only Aug 13 '18 at 7:38 # Commodore BASIC v2 (C64, VIC-20 and some PET models), 27 tokenized BASIC bytes This version uses the limited DEF FN keyword, See description here  0 DEFFNA(B)=A*B:INPUTA,B:PRINTFNA(B)  ## Commodore BASIC v2, 14 tokenized BASIC bytes  0 INPUTA,B:PRINTA*B  Both methods are likely compatible with all 8 BIT BASIC variants on Commodore home computers, but are not tested. Bourne-shell-based langs all look pretty similar: # Zsh, 11 bytes <<<$[$1*$2]


echo $[$1*$2]  # Dash/POSIX sh, 15 bytes echo$(($1*$2))

• $(( )) syntax also works for ksh93. The "fish" shell does its own thing, e.g. math "$1*\$2" – roblogic Apr 1 '19 at 23:23

# GFortran, 51 27 bytes

Fulfils the rules, I suppose integer overflow error is to be expected.

-24 thanks to Ørjan Johansen

Try It Online!

read(*,*)i,j
print*,i*j
end

• Seems you have some redundant stuff. (Unlike ab, ij default to integer.) – Ørjan Johansen Mar 27 '19 at 17:26
• Thanks! It's been about 20 years since I wrote any real Fortran :P – roblogic Mar 27 '19 at 23:37

# 3+ bytes in various methods of representing lambda calculus, taking numeral input as Church numerals.

## 19 Bytes, Untyped Lambda Calculus, using named arguments:

λx.λy.λz.x(y(z))


This is the most verbose version, . represents a lambda abstraction and parentheses represent an application

## 9 or 12 Bytes, Lambda Calculus, using De Brujin Indices:

λλλ3(2 1)


This uses De Brujin Indices instead of variable names, and function application is right-associative and implicit.

Replacing lambda symbols with backslashes (a pretty common form when Unicode is not available) yields us only 6 bytes

It's small, but we can go smaller. Using John Tromp's Binary Lambda Calculus representation, we get the binary representation 0000000111100111010, or separated into individual parts:

## 3 bytes, Untyped Lambda Calculus, using BLC representation

00 00 00 01 1110 01 110 10 xxxxx


Where the 5 xs are any padding bits.

## Explanation

These implement the simple mult function for Church Numerals in the untyped lambda calculus.

In untyped lambda calculus, EVERYTHING is a function, including numbers. All these functions take a maximum of a single argument. So how do you represent arbitrary numbers? Church encodings!

In Church encoding, numbers are represented by repeated recursion. All numbers n are represented by a function taking an argument f, and returning a function taking an argument x, which returns f(x) called n times. For example:

0: lambda f: lambda x: x
1 = lambda f: lambda x: f(x)
2 = lambda f: lambda x: f(f(x))
3 = lambda f: lambda x: f(f(f(x)))


Etc etc.

All inputs and outputs in most lambda calculus systems are in the form of church numerals.

The multiplication function takes 3 arguments and returns the first argument called on the second called on the third:

mult = lambda m: lambda n: lambda x: m(n(x))


This in lambda calculus simply multiplies the numbers m and n together.

Calling mult(2)(3) using our previous definitions of 2 and 3 results in a function:

lambda f: lambda x: f(f(f(f(f(f(x))))))


Or 6, the result of our multiplication. All the "golfs" are really just more concise ways of representing untyped lambda calculus.

# Pepe, 19 bytes

rEeeREeerEEEEeEreEE


Try it online!

Input is a,b. Change the "Separated by" box into , to work.

Explanation:

rEeeREee            # take 2 inputs (not in the same stack)
rEEEEeE     # A*B -> stack r
reEE # output as number


• Why is this non-competing? – pppery Oct 3 '19 at 3:27
• @pppery Language postdates the challenge, but I'm removing it now. – u-ndefined Oct 4 '19 at 10:21

# 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 '20 at 6:27
• @Memberfor3months yes it is :) – RGS Jun 14 '20 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.

# Groovy, 10 bytes

{x,y->x*y}


This is an unnamed closure.

Try it here!

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

## Ruby, 10 bytes

->a,b{a*b}


Answer is two characters too short without this text apparently.

# ForceLang, 36 bytes

def n io.readnum()
io.write n.mult n


Another fun use of def.

# Rust, 14

I haven't seen Rust quiet often so let's use it.

|a:u8,b:u8|a*b


This is a lambda expression that takes to 8 bit ints and multiplies them.

# Batch, 57 bytes

set "a1=%1" && set "a2=%2" && set x=%a1%*%a2% && echo %x%


&&: execute another command.

First command: Set %a1% as the first argument

Second command: Set %a2% as the second argument

Third command: Set %x% as %a1% * %a2%.

Fourth command: Type in the value of %x%.