30
\$\begingroup\$

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

Leaderboard

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>

\$\endgroup\$
23
  • 4
    \$\begingroup\$ @FlipTack That's assuming addition and multiplication are as easy in any language, which I don't know if it's actually true. \$\endgroup\$
    – Fatalize
    Commented Jan 9, 2017 at 8:13
  • 19
    \$\begingroup\$ 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. \$\endgroup\$
    – user45941
    Commented Jan 9, 2017 at 8:42
  • 35
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 9, 2017 at 9:19
  • 16
    \$\begingroup\$ Uo next: Subtract two numbers! \$\endgroup\$
    – steenbergh
    Commented Jan 9, 2017 at 12:57
  • 7
    \$\begingroup\$ @wat Leaving no barrel-bottom unscraped, eh? \$\endgroup\$
    – Gareth
    Commented Jan 10, 2017 at 12:39

144 Answers 144

1
\$\begingroup\$

brainfuck, 29 bytes

,<,[>[>+>+<<-]>[<+>-]<<-]>>>.

Try it online!

Input and output as character codes. (Example: 5*7=35)

Code:

[Tape: Y, X, temp, solution]
,<,             input X and Y
[               while Y greater than 0
  >[>+>+<<-]    add X to temp and solution
  >[<+>-]       move temp back to X
  <<-           decrement Y
]
>>>.            print solution
\$\endgroup\$
1
\$\begingroup\$

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
               
\$\endgroup\$
2
  • \$\begingroup\$ Why is this non-competing? \$\endgroup\$ Commented Oct 3, 2019 at 3:27
  • \$\begingroup\$ @pppery Language postdates the challenge, but I'm removing it now. \$\endgroup\$
    – u-ndefined
    Commented Oct 4, 2019 at 10:21
1
\$\begingroup\$

MashedPotatoes, 139 bytes

 synchronized($ARGV){
 case0.0fof{_->usestrictqw/++i/;goto*read-eval*;SETLOCAL<>}WHILEObject>STDERRs/0.0f//gWENDproc++i{`uniq-c`}{OUTPUT=<>}}

MashedPotatoes isn't on TIO, so I linked the Esolangs page for the language. It's an unusual language, so the code description might not make much sense unless you scan the info about cyclical naming conventions on the Esolangs page first. But here goes nonetheless...

I'm breaking the code into lines, but it has to be formatted as listed above to work, since the values of the labels is bound to the line number in the code.

synchronized($ARGV){              # Label block, assigns "1" to label 1
  case0.0fof{_->                  # Input block
    usestrictqw/++i/;             # Read first number into register ^A
    goto*read-eval*;              # Shift registers, ^A<-^C<-^E-<^A
    SETLOCAL<>                    # Read second number into register ^A
  }                               # Close input block
  WHILEObject>STDERRs/0.0f//gWEND # Multiply ^A*^E and store in ^E
  proc++i{`uniq-c`}{OUTPUT=<>}    # Print ^E as a number
}                                 # Close label block
\$\endgroup\$
1
\$\begingroup\$

Starry, 6 bytes

,,  *.
,,      Push input to stack (twice)
    *   Multiply
     .  Output
\$\endgroup\$
1
\$\begingroup\$

A0A0, 28 bytes

I0A1V0O0
I0V0M0
G-2G-1G-1G-1

Adapted from my answer to Add two numbers.

Each number is first put into two separate operands (V0 instructions) on two lines via the I0 instructions on each line and then later combined together for adding. After taking input, we jump to the top. The A1 adds the V0 O0 onto the next line, to get the following code: V2 M0 V1 O0. I've labelled the operands with V1 and V2 to represent the different operands. During execution these contain the actual inputs. From there it's simple.

V2 M0 V1 O0
V2          ; operand, put into the S0 next to it
   M0       ; multiplies its value to the operand next to it (V1)
      V1    ; operand, put into the O0 next to it
         O0 ; outputs its value as a number

The line of jumps at the bottom just goes back to the correct line each time.

\$\endgroup\$
1
\$\begingroup\$

Factor, 1 byte

*

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Z80 machine code, 5 4 bytes with djnz

In Machine Language:

81 10 fd c9

In Assembly:

;8-bit Multiply
;Multiplier in B, multiplicand in C, result in A.
Multiply:
    loop:
        add a, c
        djnz loop
    ret

What is djnz?

djnz decrements the B register (in this case the multiplier), and jumps a certain number of bytes forward or back (in this case, 2 bytes back; essentially jumping to the beginning of the loop), but only if, after being decremented, the B register isn't 0.

Some limitations

This subroutine can only perform 8-bit multiplication, so the result will overflow if it exceeds 0xff.

I believe I have no way of making it do 16-bit multiplication without increasing the byte count.

Without djnz

Here's the code before I added djnz:

In Machine Language:

81 05 20 fc c9

In Assembly:

;8-bit Multiply
;Multiplier in B, multiplicand in C, result in A.
Multiply:
    loop:
        add a, c
        inc b
        jr nz, loop
    ret

Testing

I used this snippet of code to test my submission:

ld c,4 
ld b,10 
call Multiply 
ld ($1000),a 
\$\endgroup\$
1
\$\begingroup\$

shell, 14 bytes

dc -e"$1 $2*p"
\$\endgroup\$
1
\$\begingroup\$

awk, 13 bytes

Simply

{print $1*$2}
\$\endgroup\$
1
\$\begingroup\$

Piet + ascii-piet, 7 bytes (5×2=10 codels)

TAJsJjj

Try Piet online!

\$\endgroup\$
1
\$\begingroup\$

Swift, 25 24 20 bytes

let t:(_,_)->Int=(*)
\$\endgroup\$
0
\$\begingroup\$

Groovy, 10 bytes

{x,y->x*y}

This is an unnamed closure.

Try it here!

\$\endgroup\$
0
\$\begingroup\$

Ruby, 10 bytes

->a,b{a*b}

Answer is two characters too short without this text apparently.

\$\endgroup\$
0
\$\begingroup\$

ForceLang, 36 bytes

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

Another fun use of def.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 13 bytes

INPUT A,B?A*B

Input should be given as two numbers, separated by a comma.

\$\endgroup\$
0
\$\begingroup\$

Powershell, 17 Bytes

param($a,$b)$a*$b

takes 2 numbers, returns them multiplied out.

\$\endgroup\$
0
\$\begingroup\$

Forth, 1 bytes

*

* pops off the top 2 items from the stack, multiplies them and pushes the result.

\$\endgroup\$
1
  • \$\begingroup\$ You can just use *, since it's a word/function. I added it to this polyglot answer \$\endgroup\$
    – mbomb007
    Commented Feb 17, 2017 at 19:07
0
\$\begingroup\$

Maple, 3 bytes

`*`

Usage:

`*`(a,b);

Returns:

a*b

Using this notation, is it possible to supply any number of arguments to the multiplication operator and it will return the product of the sequence.

\$\endgroup\$
0
\$\begingroup\$

Röda 0.11, 11 bytes

f&a,b{a*=b}

Try it online!

It modifies its first argument (which must be a variable).

Röda 0.12, 7 bytes (non-competing)

{[_*_]}

Try it online!

This kind of syntax was not supported at the time the challenge was posted. It's an anonymous function that reads to numbers from the stream

\$\endgroup\$
0
\$\begingroup\$

><>, 3 + 3 = 6 bytes

*n;

Invoke as fish.py -c '*n;' -v 2 -8, which is why I only count the flag once.

\$\endgroup\$
0
\$\begingroup\$

AHK, 15 bytes

1*=%2%
Send,%1%

1 and 2 are, by default, the first two arguments passed in to a program. Sometimes you have to escape them with percent signs so it doesn't confuse the variable 1 with the number one.

\$\endgroup\$
0
\$\begingroup\$

HEX, 69 bytes non-competing

GBL;
Listen("1");
Listen("2");
Breed("1" * "2");
Scuttle("1");
Write;

I have no idea when HEX was created, but I've listed this as non-competing as the language currently has no publicly-available functional interpreter.

\$\endgroup\$
3
  • \$\begingroup\$ ... so how can it be tested? \$\endgroup\$ Commented May 16, 2017 at 8:16
  • \$\begingroup\$ @StewieGriffin Write your own interpreter ;) \$\endgroup\$
    – Mayube
    Commented May 16, 2017 at 8:16
  • \$\begingroup\$ On a more serious note, I'm working on finishing my HEX interpreter, and while it supports GBL, Listen, Breed, Scuttle and Write, so would work for this program, it's still missing most of HEX's features, so it's not public yet. \$\endgroup\$
    – Mayube
    Commented May 16, 2017 at 8:19
0
\$\begingroup\$

Triangular, 6 bytes

$.$%*<

Try it online!

Formats into this triangle;

  $
 . $
% * <

Without directionals/no-ops, this looks like: $$*%

  • $ - read input as integer
  • * - multiply
  • % - print as integer
\$\endgroup\$
0
\$\begingroup\$

Decimal, 12 bytes

81D81D43D301

Reads two integers (separated by a space or newline), multiplies them, and prints them to STDOUT.

Ungolfed:

81D  ; builtin 1 - read INT to stack
81D  ; builtin 1 - read INT to stack
43D  ; math, multiply (postfix *)
301  ; print from stack to standard output

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Casio Basic, 6 bytes

a*b

No surprises there. 3 bytes for the program, and 3 bytes to add a,b as parameters.

\$\endgroup\$
0
\$\begingroup\$

Carrot, 5 bytes

$^F*$

Takes the input separated by newlines.

Explanation:

$ //Set the string stack to the first line of the input
^ //Change to operations mode
F //Convert to float stack
* //Multiply the stack by
$ //The second line of the input
  //Implicitly output the result
\$\endgroup\$
0
\$\begingroup\$

C, 48 bytes

main(a,b){scanf("%d%d",&a,&b);printf("%d",a*b);}

New to code golf, any suggestions are welcome.

\$\endgroup\$
0
\$\begingroup\$

cQuents, 6 bytes

#|1:AB

I really need to implement a byte that simply shorthands #|1: because every "perform this operation once" challenge uses that as the beginning.

Try it online!

Explanation

#|1      Add a 1 to the end of the user's input, it will be n
   :     Mode : (sequence 1: given n, output the nth item in the sequence)
    AB   Each item in the sequence is the first input times the second input
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.