# Print this Multiplication Table

Write the shortest code to print the following multiplication table:

1×1=1
1×2=2  2×2=4
1×3=3  2×3=6  3×3=9
1×4=4  2×4=8  3×4=12 4×4=16
1×5=5  2×5=10 3×5=15 4×5=20 5×5=25
1×6=6  2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
1×7=7  2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
1×8=8  2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9=9  2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81

• Is anyone really going to do anything besides 2 for loops? Where's the challenging (interesting) part? Commented Apr 15, 2013 at 23:56
• I don't use for. Ok, I use while. Commented Apr 16, 2013 at 13:31
• Are trailing spaces important? Commented May 13, 2013 at 19:06
• @jdstankosky My answer uses the inverted upper triangle of a scaled identity matrix :). Commented Sep 20, 2016 at 16:15
• @jdstankosky perhaps you may find my answer a bit more interesting - no loops involved Commented Jul 22, 2017 at 21:03

# PowerShell, 54 52 bytes

1..9|%{''+(1..($l=$_)|%{"$_×$l={0,-2}"-f($_*$l)})}


Try it online!

# Excel, 57 bytes

Requires Excel 365 / 2019 version 2007 or newer

An excel worksheet function that defines the contextual arrays c and r then outputs an 9x9 array containg the output.

=Let(c,Column(A:I),r,Row(1:9),If(c>r,"",r&"x"&c&"="&r*c))


# Excel VBA Version, 64 Bytes

Anonymous VBE Immediate window function that takes no input and outputs to range [A1:I9] on the ActiveSheet object

[1:9]="=Let(c,Column(),r,Row(),If(c>r,"""",r&""x""&c&""=""&r*c))


### Output

• If you put =LET(c,COLUMN(A:I),r,ROW(1:9),IF(c>r,"",r&"x"&c&"="&r*c)) into a cell, it is only 57 bytes. Commented Mar 22, 2021 at 22:23
• @Axuary, thanks - for some reason I was thinking that I already had an Excel answer to this one, but I guess I didn't I've modified my response to show this approach being used both with and without VBA Commented Mar 23, 2021 at 22:08

# ConTeXt, 67 60 bytes

\let~\dorecurse\starttext~9{~#1{##1x#1=\luaexpr{#1*##1} }

}


## Javascript: 82 characters

o="";for(a=1;a<10;a++){for(b=1;b<=a;b++){o+=a+"x"+b+"="+(a*b)+" "}o+="\n"}alert(o)

• your code can be shortened to 75 like this for(i=0,s="";9>i++;){for(j=0;j++<i;)s+=j+"x"+i+"="+j*i+" ";s+="\n"}alert(s), however the double spaces thing on 2 digits is not respected, I was about to submit that one but using +(9<i*j?" ":" ") instead of just +" " edit: on the ternary the double spaces disappeared, but they are on the second param Commented May 6, 2013 at 18:55
• I will be picky but 4x4 is not directly under 5x4 like in task. Commented Jul 26, 2016 at 13:02

## Python: 87

I'm eyeballing the solutions others have posted and most of them don't appear to get the spacing correct.

for i in range(1,10):print''.join(('%s×%s=%s'%(j,i,i*j)).ljust(7)for j in range(1,i+1))

• You have trailing spaces, does that count? ;-) Commented May 13, 2013 at 12:55

## Python (79)

or (77) if I use range(10) except that produces an empty line at the start

for i in range(1,10):print' '.join('%dx%d=%-2d'%(j,i,j*i)for j in range(1,i+1))


1x1=1
1x2=2  2x2=4
1x3=3  2x3=6  3x3=9
1x4=4  2x4=8  3x4=12 4x4=16
1x5=5  2x5=10 3x5=15 4x5=20 5x5=25
1x6=6  2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7  2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8  2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9  2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81


Perl: 65 , 62 characters

map{map{printf"%dx%d=%2d ",$_,$i,$_*$i}1..($i=$_);print$/}1..9  • Spare 2 characters by removing the printf's parenthesis; spare 1 character by moving the assignment of$i into the range expression: map{map{printf"%dx%d=%2d ",$_,$i,$_*$i}1..($i=$_);print$/}1..9. Commented Apr 17, 2013 at 7:15 • @manatwork: Thank you very much. – Toto Commented Apr 17, 2013 at 7:33 # Ruby: 79 77 characters (without loops) m=->i,j{"%dx%d=%-3d"%[i,j,i*j]+(i<j ?m[i+1,j]:j<9?$/+m[1,j+1]:$/)};$><<m[1,1]


Sample run:

bash-4.2$ruby -e 'm=->i,j{"%dx%d=%-3d"%[i,j,i*j]+(i<j ?m[i+1,j]:j<9?$/+m[1,j+1]:$/)};$><<m[1,1]'
1x1=1
1x2=2  2x2=4
1x3=3  2x3=6  3x3=9
1x4=4  2x4=8  3x4=12 4x4=16
1x5=5  2x5=10 3x5=15 4x5=20 5x5=25
1x6=6  2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7  2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8  2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9  2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81


C++, 126 characters

I took the expected approach.

#include <iostream>
int main(){for(int i=1,j;i<10;i++){for(j=1;j<=i;j++)std::cout<<i<<"x"<<j<<"="<<i*j<<" ";std::cout<<std::endl;}}


# k (63 characters)

Prints the output to stdout.

-1@/:" "/:'{7$x,"×",y,"=",z}.''$v,''(*).''v:(1+!:'t),''t:1+!9;


Example

k)-1@/:" "/:'{7$x,"×",y,"=",z}.''$v,''(*).''v:(1+!:'t),''t:1+!9;
1×1=1
1×2=2  2×2=4
1×3=3  2×3=6  3×3=9
1×4=4  2×4=8  3×4=12 4×4=16
1×5=5  2×5=10 3×5=15 4×5=20 5×5=25
1×6=6  2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
1×7=7  2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
1×8=8  2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9=9  2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81


# C, 8685 84 bytes

i;m(j){for(;i++^9;puts(""))for(j=0;j++^i;printf("%d×%d=%d %c",j,i,j*i,j*i>9?:32));}

• j*v>9?:32 what does it mean?
– user58988
Commented Sep 20, 2016 at 14:59
• jv>9?:32 it seems as jv>9?0:32 but j*v>9?32: here seems not compile...
– user58988
Commented Sep 20, 2016 at 15:28
• ideone @RosLuP
– user56344
Commented Sep 20, 2016 at 23:38
• Yes code as "a>b?9:;" I remember not compile in Ideone but code as "a>b?:9;" compile...
– user58988
Commented Sep 21, 2016 at 7:07

# VBA, 118 bytes

Sub M()
While B<9
B=1-B*(B<>A)
A=A-(B=1)
S=S &B &"x"&A &"="&A*B &IIf(A=B,vbLf,Space(2+(A*B>9)))
Wend
MsgBox S
End Sub


No for loops :-) and only one while loop.

And no If statements, so I could potentially one-line it and get the automatic "End Sub" for 8 fewer bytes if pushed; but I'm not that close to the leaderboard :-)

Sub M():While B<9:B=1-B*(B<>A):A=A-(B=1):S=S &B &"x"&A &"="&A*B &IIf(A=B,vbLf,Space(2+(A*B>9))):Wend:MsgBox S


## Convex, 27 bytes

9´{_´f{æ*'×@@'=\_s,¿)S*}N}%


Try it online!

Use the following version if only a single space is required between each equation:

## Convex, 21 bytes

9´{_´f{æ*'×@@'=\S}N}%


Try it online!

## Javascript (using external library) (74 bytes)

n=>_.Range(1,9).WriteLine(v=>_.Range(1,v).Write(" ",x=>v+"x"+x+"="+(v*x)))


Link to lib: https://github.com/mvegh1/Enumerable

Code explanation: Create a range starting from 1, for 9 elements. For each integer in the range, write a line according to the predicate accepting the current value as the input param.

Each line in WriteLine will create a range from 1 to v, and write a space delimited string where each integer in the 1 to v sequence is mapped to the multiplication equation

# Python 2, 91 characters

for k in[" ".join(["%ix%i=%i"%(j,i,j*i)for j in range(1,i+1)])for i in range(1,10)]:print k


Or, if each line as string like '1x2=2 2x2=4' is acceptable, this is 85 characters:

for k in[" ".join(["%ix%i=%i"%(j,i,j*i)for j in range(1,i+1)])for i in range(1,10)]:k


This one is slightly varied, resulting 93 characters:

print("\n".join([" ".join(["%ix%i=%i"%(j,i,j*i)for j in range(1,i+1)])for i in range(1,10)]))

• This gives me syntax errors. Which version of python are you using? Commented Jul 23, 2016 at 21:38
• Which one gave you a syntax error? All of them? Python 2.7.11 (default, Jan 22 2016, 08:29:18) [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin Commented Jul 24, 2016 at 15:43
• You're probably using Python3, in which case you should use print(k) instead of "print k" for the first solution. Commented Jul 24, 2016 at 15:45
• @nukacolacolic You should probably mention in your post or in your heading that some or all of your code only works for Python 2 and not Python 3. It helps when other people want to check your answers. Commented Jul 26, 2016 at 14:40
• You're right @Sherlock9 I'll edit the answer, thanks. Commented Jul 26, 2016 at 21:28

")for($n=0;$n++<$m;)printf("%d×%d=%-3d",$n,$m,$n*$m);  Try it online! # Kotlin, 73 bytes (1..10).forEach{(1..it).forEach{x->print("${x}x$it=${x*it} ")};println()}


Kotlin is the latest JVM language and I mainly wanted to try my hand at it.

You can try it on their online interpreter.

# Burlesque, 45 bytes

9roJcp:SOm{Jpdjimup"x="**j_+}{-]j-]==}gb)wduN


Try it online!

I'm almost positive there're multiple cuts that can be made here, but I just can't get them...

9ro     # Range [1,9]
J       # Duplicate
cp      # Cartesian product
:SO     # Filter for sorted-reversed
m{      # Map
J      # Duplicate
pd     # Product
jimup  # Convert the two numbers into a string ({1 1} => "11")
"x="** # Riffle in x=                          ("11""x=" => "1x1=")
j_+    # Append the result
}
{
-]j-] # Heads of both
==    # Are equal
}gb    # Group by first digit being the same
)wd    # Separate each internal list by spaces
uN     # Separate main list by newline and prettify


# Excel VBA, 82 Bytes

Anonymous VBE immediate window function that takes no input and outputs to the VBE immediate window.

For y=1To 9:For x=1To 9:?IIf(x>y,"",Left(x &"x"&y &"=" &x*y &"  ",7));:Next:?:Next


# Pyth, 25 bytes

Now fully conformant!

VS9jdm.[s[N\×d\=*Nd)\ 6SN


Try it online!

# Vyxal, 359 20 bytes, -j flag

Try it online

9ƛɾMƛ\×j\=+nΠ+9↲7Ẏ;∑


Finally, I'm done, and to round up LoTM, more forward-slashes. (Edit 1: the \s are gone)

• 20 bytes Commented May 10, 2021 at 11:33
• @lyxal I had 39 :p Commented May 10, 2021 at 11:33
• @Ausername honestly I just ported the 05AB1E answer Commented May 10, 2021 at 11:37

# R, 89 80 bytes

• -6 bytes golfed by pajonk and further -3 bytes by following the pajonk's hint
o=outer(x<-1:9,x,\(a,b)paste0(a,"×",b,"=",a*b))
o[lower.tri(o)]=""
write(o,1,9)


Attempt This Online!

Try it online! (smartphone-friendly output)

Despite R having almost a built-in multiplication table (the matrix of the products is as simple as 1:9%o%1:9), and dedicated functions to select the upper/lower triangular matrix, pasting and formatting takes a great deal of space...

• -6 bytes using write. Probably also possible to get rid of the t by cleverly adjusting previous code. Commented Jun 19 at 19:16
• @pajonk thanks - indeed, no transpose is needed when upper.tri is changed to the lower.tri Commented Jun 19 at 21:08

# C#, 131:

Action<string> n=x=>Console.Write(" "+x);Action f=()=>{for(int i=1,x=1;i<10;i++){for(;x<=i;x++){n(x+"x"+i+"="+i*x);}n("\n");x=1;}};


Isn't the EXACT shape, but nearly there.

• I'm not sure about the rules, but, isn't your code supposed to include all that is necessary to compile ?
– user8059
Commented May 18, 2013 at 0:36
• I think I saw somewhere that putting an action is fine in C#. Commented May 18, 2013 at 7:00

## Erlang escript 99

$cat multi main(_)->[[[io:format("~bx~b=~-3b",[B,A,A*B])||B<-s(A)],io:nl()]||A<-s(9)]. s(X)->lists:seq(1,X).  and run $ escript multi
1x1=1
1x2=2  2x2=4
1x3=3  2x3=6  3x3=9
1x4=4  2x4=8  3x4=12 4x4=16
1x5=5  2x5=10 3x5=15 4x5=20 5x5=25
1x6=6  2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7  2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8  2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9  2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81


# Javascript, ES6 65

for(i=0,r='';++i<10;r+='\n')for(e=0;e++<i;r+=i+'x'+e+'='+i*e+' ');


It was pretty fun to make!

• Nice solution. However, your code does not output anything, and it does not include the required double spaces for alignment. With these added, your solution clocks in at 85 characters: for(i=r="";10>++i;r+="\n")for(e=0;e++<i;r+=i+"x"+e+"="+i*e+(10>e*i?" ":" "));alert(r) Commented Jun 18, 2017 at 12:35

C 95 bytes

i=1,j=1;f(){printf("%dx%d=%d %c",j,i,i*j,i!=2||j>4?:32);++i>j?++j,i=1,j<=9?puts(""),f():0:f();}


they want this table:

1x1=1
2x1=2 2x2=4
3x1=3 3x2=6  3x3=9
4x1=4 4x2=8  4x3=12 4x4=16
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25
6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49
8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81


## Racket 94 bytes

(for((n(range 1 10)))(for((i(range 1(add1 n))))(printf "~ax~a=~a  "i  n(* i n)))(displayln""))


Ungolfed:

(define (f)
(for ((n (range 1 10)))
(for ((i (range 1 (add1 n))))
(printf "~ax~a=~a  " i  n (* i n)))
(displayln ""))
)


Testing:

(f)


Output:

1x1=1
1x2=2  2x2=4
1x3=3  2x3=6  3x3=9
1x4=4  2x4=8  3x4=12  4x4=16
1x5=5  2x5=10  3x5=15  4x5=20  5x5=25
1x6=6  2x6=12  3x6=18  4x6=24  5x6=30  6x6=36
1x7=7  2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49
1x8=8  2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64
1x9=9  2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81
`