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\$ – WolframH 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

13
\$\begingroup\$

Excel, 92 91 Bytes

From the VBA editor's immediate window, run the following command: Range("A1:I9").Formula="=IF(ROW()<COLUMN(),"""",COLUMN()&""×""&ROW()&""=""&COLUMN()*ROW())" The output is directly on the active worksheet. Excel output screenshot

I golfed an extra byte by swapping the order of an if to change >= to <. I didn't update the screenshot, but it only affects the formula at the top, not the output.

\$\endgroup\$
  • \$\begingroup\$ HA! I wondered when I'd see an excel answer, +1. \$\endgroup\$ – Magic Octopus Urn Sep 20 '16 at 15:54
  • 1
    \$\begingroup\$ Thanks. I think I was partially motivated by the comments about doing it without the usual nested FOR loop. \$\endgroup\$ – GuitarPicker Sep 20 '16 at 18:00
  • \$\begingroup\$ Mine was too!!! \$\endgroup\$ – Magic Octopus Urn Sep 21 '16 at 20:32
8
\$\begingroup\$

Python (75)

r=range(1,10)
for i in r:print''.join('%sx%s=%-3s'%(j,i,i*j)for j in r[:i])

a little better golfed than the other two Python versions.

\$\endgroup\$
7
\$\begingroup\$

C++, 106 98 bytes

I used two loops and a few tricks.

#import <cstdio>
main(){for(int i,j;i++-9;j=0)while(j++-i)printf("%dx%d=%d%c",j,i,i*j,j<i?32:10);}
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! Nice first post! \$\endgroup\$ – Rɪᴋᴇʀ Jul 23 '16 at 20:00
  • 1
    \$\begingroup\$ #import <stdio.h> main(){for(int i=0,j;i++-9;j=0)while(j++-i)printf("%dx%d=%d%s",j,i,i*j,j<i?"\n":" ";} is 3 bytes shorter. \$\endgroup\$ – DJMcMayhem Jul 23 '16 at 20:13
  • \$\begingroup\$ Do you need a space between #import and <cstdio>? \$\endgroup\$ – Zacharý Jul 10 '17 at 16:50
  • \$\begingroup\$ @Zacharý no that space is not needed \$\endgroup\$ – Karl Napf Aug 7 '17 at 12:39
5
\$\begingroup\$

J: 57 51 characters

([:;"2*\#"2(":@],'x',":@[,'=',":@*,' '"_)"0/~)>:i.9

No loops.

\$\endgroup\$
  • \$\begingroup\$ Conjunctions (and adverbs) have higher precedence than verbs so you can remove 3 pairs of brackets. ([:;"2*\#"2(":@],'x',":@[,'=',":@*,' '"_)"0/~)>:i.9 \$\endgroup\$ – randomra Apr 16 '13 at 21:06
  • \$\begingroup\$ @randomra Good call. Thanks for the tip! \$\endgroup\$ – SL2 Apr 16 '13 at 21:12
4
\$\begingroup\$

APL (37)

∆∘.{⊃(⍺≥⍵)/,/(⍕⍺)'×'(⍕⍵)'=',⍕⍺×⍵}∆←⍳9

And it's not even just two for-loops. In APL, the following construct:

x ∘.F y

where x and y are lists, and F is a function, applies F to each pair of items in x and y and gives you a matrix.

So: ∆∘.×∆←⍳9 gets you a multiplication table from 1 to 9. The above function generates the required string for each pair, i.e. (⍕⍺), string representation of the first number, followed by ×, followed by (⍕⍵), string representation of the second number, followed by =, followed by ⍕⍺×⍵, as long as ⍺≥⍵.

\$\endgroup\$
4
\$\begingroup\$

Ruby: 60 59 characters

1.upto(9){|i|puts (1..i).map{|j|"%dx%d=%-3d"%[j,i,i*j]}*""}

Sample run:

bash-4.2$ ruby -e '1.upto(9){|i|puts (1..i).map{|j|"%dx%d=%-3d"%[j,i,i*j]}*""}'
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\$
  • \$\begingroup\$ Could save one character by changing the initial (1..9).map{ into 1.upto(9){! \$\endgroup\$ – Paul Prestidge Apr 17 '13 at 23:46
  • \$\begingroup\$ There are trailing spaces (first 3 lines). The original table doesn't have them. I'm not sure if that makes a difference, though... \$\endgroup\$ – WolframH May 13 '13 at 12:51
  • \$\begingroup\$ @WolframH, all solutions I checked either have trailing spaces or not reproduce the exact formatting. \$\endgroup\$ – manatwork May 13 '13 at 13:03
4
\$\begingroup\$

Perl, 54 Characters

printf"%dx$?=%-3d"x$?.$/,map{$_,$_*$?}1..$?while$?++<9
\$\endgroup\$
4
\$\begingroup\$

APL (Dyalog), 28

↑{⍵{⍕⍵,'×',⍺,'=',⍺×⍵}¨⍳⍵}¨⍳9

Analogous to a double loop in other languages

{⍵{...}¨⍳⍵}¨⍳9 sets up the double loop
⍕⍵,'×',⍺,'=',⍺×⍵ creates the string for each pair
Convert array of array of strings to a matrix of stings

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\$ you could remove some of the commas to the same effect: ↑{⍵{⍕⍵'×'⍺'=',⍺×⍵}¨⍳⍵}¨⍳9 or even make use of the new "key operator": {⍕⍵'×'⍺'=',⍺×⍵}¨∘⍳⌸⍳9 \$\endgroup\$ – ngn Feb 4 '18 at 18:12
4
\$\begingroup\$

Mathematica, 45

Pretty boring, but I guess it serves as a syntax comparison:

Grid@Table[Row@{a, "x", b, "=", a b}, {a, 9}, {b, a}]
\$\endgroup\$
  • 2
    \$\begingroup\$ What, you're telling me there isn't a builtin for this? \$\endgroup\$ – Aaron Jul 26 '16 at 13:26
  • 1
    \$\begingroup\$ @Aaron The function bloat of Mathematica doesn't extent that far yet, thankfully. \$\endgroup\$ – Mr.Wizard Jul 26 '16 at 16:08
4
\$\begingroup\$

D, 75 chars

foreach(i,1..10){foreach(j,1..i+1){writef("%dx%d=%d ",i,j,i*j);}writeln();}

you just said code not function or full program

\$\endgroup\$
  • 1
    \$\begingroup\$ By default, snippets are not allowed; a function or full program is required. \$\endgroup\$ – a spaghetto Jul 26 '16 at 21:06
  • \$\begingroup\$ @quartata This answer predates our defaults by a year and a half. \$\endgroup\$ – Dennis Jul 26 '16 at 22:43
  • \$\begingroup\$ @Dennis Oh, I didn't realize the no snippets allowed rule was that new. Sorry. \$\endgroup\$ – a spaghetto Jul 26 '16 at 23:12
  • \$\begingroup\$ Would this work? foreach(i,1..10){foreach(j,1..i+1)writef("%dx%d=%d ",i,j,i*j);writeln;} \$\endgroup\$ – Zacharý Jul 10 '17 at 16:52
3
\$\begingroup\$

VBScript (133); without loops.

g=""
sub m(x,y)
    g=x&"x"&y&"="&x*y&vbTab&g
    if x>1 then 
        m x-1,y
    elseif y>1 then 
        g=vbLf&g 
        m y-1,y-1
    end if
end sub
m 9,9
wscript.echo g

On request of the challenger: no loops. This code uses recursive subroutine calls.

\$\endgroup\$
3
\$\begingroup\$

Maple, 64

seq(printf(seq(printf("%ax%a=%a ",j,i,i*j),j=1..i),"\n"),i=1..9)
\$\endgroup\$
3
\$\begingroup\$

x86_64 machine code (linux), 175 99 76 bytes

0000000000400080 <_start>:
  400080:   66 bf 09 00             mov    $0x9,%di

0000000000400084 <_table.L2>:
  400084:   6a 0a                   pushq  $0xa
  400086:   89 fe                   mov    %edi,%esi

0000000000400088 <_table.L3>:
  400088:   89 f0                   mov    %esi,%eax
  40008a:   f7 e7                   mul    %edi

000000000040008c <_printInteger>:
  40008c:   6a 20                   pushq  $0x20
  40008e:   3c 0a                   cmp    $0xa,%al
  400090:   7d 02                   jge    400094 <_printInteger.L1>
  400092:   6a 20                   pushq  $0x20

0000000000400094 <_printInteger.L1>:
  400094:   66 31 d2                xor    %dx,%dx
  400097:   b3 0a                   mov    $0xa,%bl
  400099:   66 f7 f3                div    %bx
  40009c:   83 c2 30                add    $0x30,%edx
  40009f:   52                      push   %rdx
  4000a0:   66 85 c0                test   %ax,%ax
  4000a3:   75 ef                   jne    400094 <_printInteger.L1>
  4000a5:   6a 3d                   pushq  $0x3d
  4000a7:   66 57                   push   %di
  4000a9:   80 04 24 30             addb   $0x30,(%rsp)
  4000ad:   6a 78                   pushq  $0x78
  4000af:   66 56                   push   %si
  4000b1:   80 04 24 30             addb   $0x30,(%rsp)
  4000b5:   ff ce                   dec    %esi
  4000b7:   75 cf                   jne    400088 <_table.L3>
  4000b9:   ff cf                   dec    %edi
  4000bb:   75 c7                   jne    400084 <_table.L2>

00000000004000bd <_printChars>:
  4000bd:   66 ba 00 08             mov    $0x800,%dx
  4000c1:   b0 01                   mov    $0x1,%al
  4000c3:   66 bf 01 00             mov    $0x1,%di
  4000c7:   48 89 e6                mov    %rsp,%rsi
  4000ca:   0f 05                   syscall

This is a dump of the binary file, and all of this is 175 bytes. It basically does the same two loops that all the answers do, but printing to the console is a bit harder and basically requires pushing the characters to print onto the stack in reverse, and then making a (linux specific) syscall to actually put those chars into stdout.

I've now optimized this so that only 1 write operation is performed (faster!) and has magic numbers (wow!) and by pushing the entire result onto the stack backwards before making the syscall. I also took out the exit routine because who needs proper exit code?

Here's a link to my first and second attempts, in their original nasm syntax.

I welcome anyone who has any other suggestions on how it can be improved. I can also explain the logic in more detail if anyone is curious.

(Also, it doesn't print the extra spaces to make all the columns aligned, but if that's required I can put the logic in at the cost of a few more bytes).

EDIT: Now prints extra spaces and is golfed down even more! It's doing some pretty crazy stuff with the registers, and is probably unstable if this program were to be expanded.

\$\endgroup\$
  • \$\begingroup\$ PPCG requires full programs or functions. Snippets are implicitly disallowed (i.e. you can use them only if the OP has explicitly allowed them.) \$\endgroup\$ – Erik the Outgolfer Jul 26 '16 at 13:57
  • \$\begingroup\$ Oh, my bad. I forgot OP hadn't specified that. \$\endgroup\$ – davey Jul 26 '16 at 20:59
3
\$\begingroup\$

Javascript, 190 bytes

Late to the party, but I was piqued by @jdstankosky 's comment and decided to take a different approach. Here's a Javascript entry that mauls a template and evals itself along the way.

t="a*b=c ";u="";r=u;for(i=1;i<10;i++){a=0;u=u+t;r+=u.split(' ').map(x=>x.replace('a',++a).replace('b',i)).map(x=>x.replace('*','x').replace('c',eval(x.substr(0,3)))).join(' ')+'\n'}alert(r);

Un-golfed version (slightly older version in which a function returns the table instead of a script alerting it, but the same principles apply):

function f()
{
    t="a*b=c "; // template for our multiplication table
    u="";r="";  // tmp- and return values
    for(i=1;i<10;i++)
    {
        a=0;    // is auto-incremented in MAP
        u=u+t;// extend the template once per iteration
        v=u.split(' '); // Smash the template to pieces
        w=v.map(x=>x.replace('a', ++a).replace('b', i)) // MAP replaces the A and B's with the correct numbers
        w=w.map(x=>x.replace('*', 'x').replace('c', eval(x.substring(0,3)))).join(' '); // second map evals that and replaces c with the answer, makes the asteriks into an X
        r=r+w+'\n'  // results get concatenated
    }
    return r;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ I made that comment a looong time ago, haha. I'm actually glad to see this. \$\endgroup\$ – jdstankosky Aug 10 '17 at 13:37
3
\$\begingroup\$

Pascal, 128 bytes

One recursive procedure takes care of everything. Call with m(9,9).

procedure m(i,j:integer);begin if i<1then Exit;if i=j then begin m(i-1,j-1);writeln;end;m(i-1,j);write(i,'x',j,'=',i*j,' ');end;

Ungolfed:

procedure mul(i, j: integer);
begin
  if i<1 then
    Exit;
  if i=j then
  begin
    mul(i-1, j-1);
    writeln;
  end;
  mul(i-1, j);
  write(i,'x',j,'=',i*j,' ');
end;
\$\endgroup\$
3
\$\begingroup\$

Fourier, 756 632 bytes

Thanks @BetaDecay for 124 bytes!

1o120~Ea1o61a1o10~Na1oEa2o61a2o32~Saa2oEa2o61a4oNa1oEa3o61a3oSaa2oEa3o61a6oSaa3oEa3o61a9o^a1oEa4o61a4oSaa2oEa4o61a8oSaa3oEa4o61a12oSa4oEa4o61a16oNa1oEa5o61a5oSaa2oEa5o61aNoSa3oEa5o61a15oSa4oEa5o61a20oSa5oEa5o61a25oNa1oEa6o61a6oSaa2oEa6o61a12oSa3oEa6o61a18oSa4oEa6o61a24oSa5oEa6o61a30oSa6oEa6o61a36oNa1oEa7o61a7oSaa2oEa7o61a14oSa3oEa7o61a21oSa4oEa7o61a28oSa5oEa7o61a35oSa6oEa7o61a42oSa7oEa7o61a49oNa1oEa8o61a8oSaa2oEa8o61a16oSa3oEa8o61a24oSa4oEa8o61aSoa5oEa8o61a40oSa6oEa8o61a48oSa7oEa8o61a56oSa8oEa8o61a64oNa1oEa9o61a9oSaa2oEa9o61a18oSa3oEa9o61a27oSa4oEa9o61a36oSa5oEa9o61a45oSa6oEa9o61a54oSa7oEa9o61a63oSa8oEa9o61a72oSa9oEa9o61a81o
\$\endgroup\$
2
\$\begingroup\$

vba 55

(immediate window)

for f=1 to 9:for j=1 to f:?f;"x";j;"=";f*j,:next:?:next

note - GWBasic only needs 2 extra characters:

1 for f=1 to 9:for j=1 to f:?f;"x";j;"=";f*j,:next:?:next
\$\endgroup\$
2
\$\begingroup\$

Javascript, 75

for(s="",a=b=1;a<10;b=a==b?(a++,alert(s),s="",1):b+1)s+=b+"x"+a+"="+a*b+" "

I wonder if something better than two (combined?) for loops is possible...

\$\endgroup\$
  • \$\begingroup\$ well, the only thing I am sure is that it is possible to get 75 on separated loops (my old comment) \$\endgroup\$ – ajax333221 May 6 '13 at 19:14
2
\$\begingroup\$

Coreutils/Bash: 147 136 135

for i in {1..9}; do
  yes $'\n' | head -n $[i-1] > $i
  paste -dx= <(yes $i) <(seq $i 9) <(seq $[i*i] $i $[9*i]) |head -n$[10-i] >> $i
done
paste {1..9}

Golfed, using explicit newline and, using deprecated head option (thanks manatwork):

for i in {1..9};do yes '
'|head -$[i-1]>$i;paste -dx= <(yes $i) <(seq $i 9) <(seq $[i*i] $i $[9*i])| head -$[10-i]>>$i;done;paste {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\$
  • \$\begingroup\$ Spare 8 characters by replacing all $(( )) arithmetic evaluations with $[ ]; share 2 characters by replacing the $'\n' escaped newline character with a literal one (I mean, yes ' in one line, then ' in the following one); spare 2 characters by not using head's -n option explicitly, just - and the number. \$\endgroup\$ – manatwork Apr 17 '13 at 7:24
  • \$\begingroup\$ @manatwork: I didn't know about the $[ ] notation, good to know. Replacing -n by - is only one character less so it's 11 in total, thank you very much :). \$\endgroup\$ – Thor Apr 17 '13 at 7:56
2
\$\begingroup\$

LOLCODE, 202 bytes

IM IN YR o UPPIN YR b TIL BOTH SAEM b AN 10
c R ""
IM IN YR i UPPIN YR a TIL BOTH SAEM a AN SUM OF b AN 1
c R SMOOSH c SMOOSH a "x" b "=" PRODUKT OF a AN b " " MKAY
IM OUTTA YR i
VISIBLE c
IM OUTTA YR o

Ungolfed:

HAI 1.3 BTW Unnecessary in current implementations
IM IN YR outer UPPIN YR multiplicand TIL BOTH SAEM multiplicand AN 10
    I HAS A output ITZ ""
    IM IN YR inner UPPIN YR multiplier TIL BOTH SAEM multiplier AN SUM OF multiplicand AN 1
        output R SMOOSH output AN SMOOSH multiplier AN "x" AN multiplicand AN "=" AN PRODUCKT OF multiplicand AN multiplier AN " " MKAY MKAY BTW AN is optional to separate arguments, a linebreak is an implicit MKAY.
    IM OUTTA YR inner
    VISIBLE output
IM OUTTA YR outer
KTHXBYE BTW Unnecessary in current implementations

Pythonated for non-leetspeakers:

for multiplicand in range(1, 10):
    output = ""
    for multiplier in range(1, multiplicand + 1):
        output = output + (multiplier + "x" + multiplicand + "=" + str(multiplicand * multiplier) + " ")
    print(output)
\$\endgroup\$
  • \$\begingroup\$ As someone who's also used LOLCODE in a code challenge submission, have my upvote! LOVE this lang \$\endgroup\$ – jdstankosky Aug 10 '17 at 13:39
2
\$\begingroup\$

c#, 142 bytes

Enumerable.Range(1,9).ToList().ForEach(i =>Enumerable.Range(1,i).ToList().ForEach(j=>Console.Write("{0}x{1}={2}{3}",j,i,j*i,j==i?"\n":"\t")));

And not a for in sight...

\$\endgroup\$
  • \$\begingroup\$ ForEach "not a for in sight" well... xD \$\endgroup\$ – HyperNeutrino Sep 24 '17 at 17:59
2
\$\begingroup\$

><>, 50 bytes

1v
 1
?\::n"x"o{::n"="o}*n" "o1+:{:})
 \~1+:a=?;ao

You can try it on the online interpreter.

Note that there is trailing spaces on each lines, which might make it incorrect (OP hasn't stated on this point as of this answer).

\$\endgroup\$
2
\$\begingroup\$

///, 268 bytes

/_/\/\///x/×_N/x9=_E/x8=_V/x7=_S/x6=_F/x5=_R/x4=_O/
1_t/  2_h/ 3/1x1=1Ox2=2tx2=4Ox3=3tx3=6 hx3=9OR4tR8 hR12 4R16OF5tF10hF15 4F20 5F25OS6tS12hS18 4S24 5S30 6S36OV7tV14hV21 4V28 5V35 6V42 7V49OE8tE16hE24 4E32 5E40 6E48 7E56 8E64ON9tN18hN27 4N36 5N45 6N54 7N63 8N72 9N81
\$\endgroup\$
2
\$\begingroup\$

JAVA, 103 94 92 90 bytes

Using JShell from Java 9 SDK allows me to save large amount of space

for(int i=0,j;i++<9;)for(j=1;j<=i;)System.out.print(i+"*"+j+"="+i*j+"\t"+(j++<i?"":"\n"))

Following Kevin's suggestion I reduced solution by 2 bytes.

Thanks to cliffroot, I was able to reduce it by another 1 byte

\$\endgroup\$
  • 1
    \$\begingroup\$ You can save a few bytes by removing the int from the second for-loop, and add ,j to the first. So like this: for(int i=0,j;++i<=9;)for(j=1;j<=i;)System.out.print(i+"*"+j+"="+i*j+"\t"+(j++<i?"":"\n")); \$\endgroup\$ – Kevin Cruijssen Jul 26 '16 at 12:31
  • \$\begingroup\$ It seems like you can replace ++i<=9 with i++<9 \$\endgroup\$ – cliffroot Jul 26 '16 at 14:06
2
\$\begingroup\$

C 79 bytes

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

the main

main(){f();}

the 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\$
2
\$\begingroup\$

Tcl 98 chars

while {[incr a]<10} {set b 0;while {[incr b]<=$a} {puts -nonewline "$a×$b=[expr $a*$b] "};puts ""}
\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – ajax333221 May 6 '13 at 18:55
  • \$\begingroup\$ I will be picky but 4x4 is not directly under 5x4 like in task. \$\endgroup\$ – user902383 Jul 26 '16 at 13:02
1
\$\begingroup\$

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))
\$\endgroup\$
  • \$\begingroup\$ You have trailing spaces, does that count? ;-) \$\endgroup\$ – WolframH May 13 '13 at 12:55
1
\$\begingroup\$

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

Perl: 65 , 62 characters

map{map{printf"%dx%d=%2d ",$_,$i,$_*$i}1..($i=$_);print$/}1..9
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – manatwork Apr 17 '13 at 7:15
  • \$\begingroup\$ @manatwork: Thank you very much. \$\endgroup\$ – Toto Apr 17 '13 at 7:33

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.