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 2 3 4
5
0
\$\begingroup\$

Tcl, 10 bytes

expr $a*$b

Try it online!


If one states

namespace path tcl::mathop

before then * acts like a procedure:

Tcl, 7 bytes

* $a $b

Try it online!

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

QC 4 bytes

QQGN

Requires a newer version which must be compiled manually.

Q Read number from stdin and push to stack
Q Same as above
G Multiply two numbers from stack
N Pop and print number from stack
\$\endgroup\$
0
0
\$\begingroup\$

[C64, BasicV2], 11 bytes

1rEA,B:?A*B

The input can be given like:

0dA45,45
\$\endgroup\$
2
  • \$\begingroup\$ 0 INPUTA,B:?A*B Note that Commodore BASIC keyword abbreviations doesn't save BASIC bytes, only typing \$\endgroup\$ Commented Jan 16, 2018 at 10:30
  • \$\begingroup\$ @ShaunBebbers As far I know, it is opposite: Basic programs are stored in a tokenized form in the memory, thus even if you write the whole PRINT command, it will be stored as ?. It shows as PRINT only if you LIST it. But it is not very important, because the task is to minimize the code size, not the basic byte size. \$\endgroup\$
    – peterh
    Commented Jan 16, 2018 at 10:42
0
\$\begingroup\$

Batch File, 23 bytes

@set/ak=%1*%2
@echo %k%

Batch doesn't allow you to print the result of an operation; Thus, we are required to create a temporary variable k which contains the result of the multiplication between argument 1 and 2.

The @'s in front of the commands prevent the current path from being printed before each line on the command prompt.

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

Falcon, 34 bytes

a=int(input())
b=int(input())
>a*b

Try it online!

Falcon uses a similar syntax to Python. Its output, however, has these awesome aliases called "fast-printing" (as per the documentation):

  • printl() can be written as >

  • print() can be written as >>

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

Excel VBA, 8 Bytes

Takes input from cells A1 and B1 and outputs to the VBA immediates window

?[A1*B1]

which is functionally equivalant to

Print ActiveSheet.Range("A1").Value * ActiveSheet.Range("B1").Value
\$\endgroup\$
0
\$\begingroup\$

Verbosity, 392 bytes

Include<Input>
Include<Output>
Include<Integer>
Include<MetaFunctions>
Input:DefineVariable<i; 0>
Output:DefineVariable<o; 0>
Integer:DefineVariable<f; Input:ReadEvaluatedLineFromInput<i>>
Integer:DefineVariable<s; Input:ReadEvaluatedLineFromInput<i>>
Integer:DefineVariable<r; Integer:Product<f; s>>
Output:DisplayAsText<o; r>
DefineMain<> [
MetaFunctions:ExecuteScript<MetaFunctions@FILE>
]

Try it online!

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

Whispers, 35 bytes

> Input
> Input
>> 1×2
>> Output 3

Try it online!

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

Pyth - 5 bytes

AQ*GH

Explanation:

AQ*GH
A     Set G and H to the first two elements of
 Q    Evaluated input
      Implicitly print
   G  G
  *   Times
    H H
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 3 bytes \$\endgroup\$
    – hakr14
    Commented Aug 13, 2018 at 3:07
0
\$\begingroup\$

Implicit, 1 byte

*

Implicit input of two integers, multiply them, implicit output. Try it online!

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

Wumpus, 5 bytes

II*O@

Try it online!

Explanation

Straight-forward and boring:

I   Read the first integer.
I   Read the second integer.
*   Multiply them.
O   Output the result.
@   Terminate the program.
\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 9 bytes

a=>b=>a*b

Try it online!

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

Red, 16 bytes

func[a b][a * b]

Try it online!

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

T-SQL, 17 bytes

SELECT a*b FROM t

Per our IO rules, SQL can take input from a pre-existing table t with integer columns a and b.

This has the bonus of working on an unlimited* number of provided pairs, if they are included in the input table.

*Well, limited only by SQL storage bounds, which are pretty darn high, using enterprise versions.

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

Cubically, 8 7 bytes

$:7$*7%

Explanation:

$        read input
 :7      set notepad to input
   $     read input
    *7   multiply notepad by input
      %  print result

Try it online!

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

Python 3, 14 bytes

lambda x,y:x*y

It's literally just multiplication, lol

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

Braingolf, 1 byte

*

Try it online!

Implicit input, * multiplies the last 2 number on the stack and pushes the result, implicit output.

Here's a slightly more interesting one:

Braingolf, 9 bytes

<2->[.]&+

Try it online!

Implicit input of x and y. Subtracts 2 from x, (because [] is a do-while and we already have one y), then duplicates y x times, finally, sums the entire stack. Implicit output.

\$\endgroup\$
0
\$\begingroup\$
gawk '($_*=$2)_'

The final underscore (..)_ is the most succinct way I could think of to force concat an empty string to the zeros and coerce it into a string in order to print it without using the print statement.

weak typing in awk is advantageous cuz I never defined _, so 1st one is used as numeric zero while 2nd one acts as empty string.

2
20
63
-16
-72
72
0
0
0
0
0
\$\endgroup\$
0
\$\begingroup\$

ARBLE, 3 bytes

a*b

Similar to the answer for Adding two numbers.

Try it online!

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

HP‑41C series, 7 B

Input is to be placed in the X and Y stack registers.

01♦LBL⸆P           5 Bytes  global label requires 4 + (length of string) Bytes
02 ∗               1 Byte   X ≔ Y × X
03 RTN             1 Byte   `RTN` does not affect local label search

Some other (longer) methods:

  • stack arithmetic:
    ST∗ Y            2 Bytes  Rₙ ≔ Rₙ × X
    
    Result is in Y now. Necessary keystrokes: STO × . × (for the letter Y).
  • statistics functions:
    CLΣ              1 Byte   clear statistics registers
    Σ+               1 Byte   accumulate statistics values
    
    If ΣREG? = 11 (the default), result is in R15.
  • Furthermore, there’s % (1 Byte), but it will require scaling the result back by a factor of 100.
\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 1 byte

×

Attempt This Online!

No, * is exponentiation, not multiplication.

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

Desmoslang Assembly, 5 Bytes

I*IOT

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

ReRegex, 58 bytes

#import math
-(.*?)-/$1/(.+)-/-$1/(\d*)x(\d*)/$1*$2/#input

Try it online!

Ungolfed

#import math
-(.*?)-/$1/#		# Remove pairs of -
(.+)-/-$1/#		# Move any - to the start of the text
(\d*)x(\d*)/$1*$2/#	# Take the input of (number)x(number) and convert it to (number)*(number), implicitly multiplying.
#input

Slightly more interesting, 81 bytes

-(.*?)-/$1/(.+)-/-$1/(?<![:_])(_*)x(_*)/:$1x$2/:(_*)x(_*)_/$1:$1x$2/:_*x$//#input

Try it online!

Ungolfed

-(.*?)-/$1/#			# Remove pairs of -
(.+)-/-$1/#			# Move any - to the start of the text
(?<![:_])(_*)x(_*)/:$1x$2/#	# Add a : if there isn't one, to keep track of the output
:(_*)x(_*)_/$1:$1x$2/#		# Whilst the second argument is > 0, remove one from it, and copy the first argument to the output
:_*x$//#			# When the second argument is zero, remove all the supporting stuff, leaving just the output
#input
\$\endgroup\$
-2
\$\begingroup\$

Ruby, 10 bytes

->a,b{a*b}

and it would be great to learn if it is possible with fewer bytes

\$\endgroup\$
5
1 2 3 4
5

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.