17
\$\begingroup\$

Write 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
\$\endgroup\$
  • 6
    \$\begingroup\$ Is anyone really going to do anything besides 2 for loops? Where's the challenging (interesting) part? \$\endgroup\$ – jdstankosky Apr 15 '13 at 23:56
  • 3
    \$\begingroup\$ I don't use for. Ok, I use while. \$\endgroup\$ – Johannes Kuhn Apr 16 '13 at 13:31
  • 3
    \$\begingroup\$ Are trailing spaces important? \$\endgroup\$ – Reinstate Monica May 13 '13 at 19:06
  • \$\begingroup\$ why in the first column there are 2 spaces and not 1? (as the other colums ) \$\endgroup\$ – RosLuP Sep 20 '16 at 15:21
  • 1
    \$\begingroup\$ @jdstankosky perhaps you may find my answer a bit more interesting - no loops involved \$\endgroup\$ – Taylor Scott Jul 22 '17 at 21:03

63 Answers 63

1
\$\begingroup\$

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 
\$\endgroup\$
1
\$\begingroup\$

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;}}
\$\endgroup\$
1
\$\begingroup\$

Python 2, 72

i=1;exec"j=1;exec'print\"%sx%s=%-2s\"%(j,i,j*i),;j+=1;'*i;print;i+=1;"*9

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
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

C, 86 85 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));}
\$\endgroup\$
  • \$\begingroup\$ j*v>9?:32 what does it mean? \$\endgroup\$ – RosLuP Sep 20 '16 at 14:59
  • \$\begingroup\$ jv>9?:32 it seems as jv>9?0:32 but j*v>9?32: here seems not compile... \$\endgroup\$ – RosLuP Sep 20 '16 at 15:28
  • \$\begingroup\$ ideone @RosLuP \$\endgroup\$ – o79y Sep 20 '16 at 23:38
  • \$\begingroup\$ Yes code as "a>b?9:;" I remember not compile in Ideone but code as "a>b?:9;" compile... \$\endgroup\$ – RosLuP Sep 21 '16 at 7:07
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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!

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

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

enter image description here

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

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)]))
\$\endgroup\$
  • 1
    \$\begingroup\$ This gives me syntax errors. Which version of python are you using? \$\endgroup\$ – DJMcMayhem Jul 23 '16 at 21:38
  • \$\begingroup\$ 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 \$\endgroup\$ – eaydin Jul 24 '16 at 15:43
  • \$\begingroup\$ You're probably using Python3, in which case you should use print(k) instead of "print k" for the first solution. \$\endgroup\$ – eaydin Jul 24 '16 at 15:45
  • \$\begingroup\$ @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. \$\endgroup\$ – Sherlock9 Jul 26 '16 at 14:40
  • \$\begingroup\$ You're right @Sherlock9 I'll edit the answer, thanks. \$\endgroup\$ – eaydin Jul 26 '16 at 21:28
1
\$\begingroup\$

Groovy, 65 60 characters

1.upto(9){x->1.upto(x){printf(x+"x$it=%-3d",x*it)}println()}

Sample run:

bash-4.3$ groovy -e '1.upto(9){x->1.upto(x){printf(x+"x$it=%-3d",x*it)}println()}'
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 
\$\endgroup\$
1
\$\begingroup\$

golflua, 54 49 characters

~@i=1,9~@j=1,i I.w(S.q(i.."x%d=%-3d",j,i*j))$w()$

Sample run:

bash-4.3$ golflua -e '~@i=1,9~@j=1,i I.w(S.q(i.."x%d=%-3d",j,i*j))$w()$'
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 
\$\endgroup\$
1
\$\begingroup\$

Julia (236 Bytes, potentially 63) - Could use help.

A=[1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9;1 2 3 4 5 6 7 8 9]
map(x->x>0?print(x,"x",x,"=",x*x,"\t"):println(),triu!(A'))

Someone put in the comments "Who's going to do anything but use 2 loops?"

This guy is!

I don't know how to instantiate matrices easily in Julia, anyone who could help me do the instantiation of the matrix better I will be grateful. I can only get it to make a 2D array, and triu doesn't work on that.

How it works:

  • A = [...] creates a 9x9 matrix with 1-9 in each vector (someone please teach me to do this right without accidentally creating a 2D array... I want to learn this language better, but I'm confused on the typing differences and a lot of the documentation is pretty terse for matrices.
  • triu!(A') takes the inverse of the upper triangle of the matrix.
  • map(x->x?0...:...,A) mapping function that differentiates on x=0, printing a newline when needed.

Output:

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
\$\endgroup\$
  • \$\begingroup\$ That was my comment! Lol, it's been some years. \$\endgroup\$ – jdstankosky Aug 10 '17 at 13:33
1
\$\begingroup\$

Pyke, 17 16 bytes (noncompeting)

Pyke was created after this question was asked

TFjSF\xj\=ij*s(P

Try it here!

TFj           (  - for j in range(10):
   SF         (  -     for i in range(1,j+1):
     \xj\=ij*s   -         sum(i,"x",j,"=",i*j)
               P - pretty_print(^)
\$\endgroup\$
1
\$\begingroup\$

PHP, 73 bytes

for(;$m++<9;print"
")for($n=0;$n++<$m;)printf("%d×%d=%-3d",$n,$m,$n*$m);

Try it online!

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

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.

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

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.

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

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
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ 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) \$\endgroup\$ – Tomas Langkaas Jun 18 '17 at 12:35
0
\$\begingroup\$

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 
\$\endgroup\$
0
\$\begingroup\$

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  
\$\endgroup\$
0
\$\begingroup\$

tcl, 93

time {incr i;set j 0;while {[incr j]<=$i} {puts -nonewline $j\x$i=[expr $j*$i]\ };puts ""} 10

demo

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

///, 262 bytes

/;/=4//:/x\/\///-/ 6:,/=2//'/ 5:&/=1//%/ 4:#/ 3:"/
1:!/  2x/1x1&"2,!2;"3=3!3=6 #3=9"4;!4=8 #4&2%4&6"5=5!5&0#5&5%5,0'5,5"6=6!6&2#6&8%6,4'6=30-6=36"7=7!7&4#7,1%7,8'7=35-7;2 7x7;9"8=8!8&6#8,4%8=32'8;0-8;8 7x8=56 8x8=64"9=9!9&8#9,7%9=36'9;5-9=54 7x9=63 8x9=72 9x9=81

Try it online!

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

Haskell, 84 82 bytes

putStr$init$unlines[unwords[s i++'×':s j++'=':s(i*j)|i<-[1..j]]|j<-[1..9]]
s=show

-2 thanks to @Laikoni!

Try it online!

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

Perl 5, 51 bytes

for$/(1..9){printf$_."x$/=%-3d",$_*$/for 1..$/;say}

Try it online!

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

Excel VBA, 88 76 74 73 Bytes

No For loops

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

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

-12 Bytes for removing A1 in all Column(...) and Row(...) calls

-2 Bytes for changing [A1:I9] to [1:9]

Output

enter image description here

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

uBASIC, 87 bytes

Anonymous function that takes no input and outputs to the console.

0ForY=1To9:ForX=1To9:z=x*y:Ifx<=yThen?x;"x";y;"=";z;" ";:Ifz<10Then?" ";
2NextX:?:NextY

Try it online!

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

Excel VBA, 83 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
\$\endgroup\$
0
\$\begingroup\$

MY-BASIC, 105 bytes

Anonymous function that takes no input and prints a multiplication table to the console.

Str(...) conversions are costly in this solution.

For Y=1 To 9
For X=1 To 9
If x<=y Then Print Left(Str(x)+"x"+Str(y)+"="+Str(x*y)+"  ",7)
Next
Print;
Next

Try it online!

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

Python 3.6, 70 68 bytes

r=range(1,10)
for b in r:print(*[f"{a}x{b}={a*b:<2}"for a in r[:b]])

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I had not seen Daniel's Answer before submitting this, but credit to him for the original implementation of this idea. \$\endgroup\$ – GarethPW Jul 9 '17 at 21:09
  • 1
    \$\begingroup\$ 68 bytes \$\endgroup\$ – ovs Jan 31 '18 at 19:31
0
\$\begingroup\$

Yabasic, 101 bytes

Anonymous function that takes no input and prints a multiplication table to the console.

For y=1To 9
For x=1To 9
If(x<=y)Then
?Left$(Str$(x)+"x"+Str$(y)+"="+Str$(x*y)+"  ",7);
Fi
Next
?
Next

-3 bytes for the use of Fi over EndIf

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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