# Tutorial :Code-golf: Output multiplication table to the Console

### Question:

I recently pointed a student doing work experience to an article about dumping a multiplication table to the console. It used a nested for loop and multiplied the step value of each.

This looked like a .NET 2.0 approach. I was wondering, with the use of Linq and extension methods,for example, how many lines of code it would take to achieve the same result.

Is the stackoverflow community up to the challenge?

The challenge: In a console application, write code to generate a table like this example:

`  01 02 03 04 05 06 07 08 09  02 04 06 08 10 12 14 16 18  03 06 09 12 15 18 21 24 27  04 08 12 16 20 24 28 32 36  05 10 15 20 25 30 35 40 45  06 12 18 24 30 36 42 48 54  07 14 21 28 35 42 49 56 63  08 16 24 32 40 48 56 64 72  09 18 27 36 45 54 63 72 81  `

As this turned into a language-agnostic code-golf battle, I'll go with the communities decision about which is the best solution for the accepted answer.

There's been alot of talk about the spec and the format that the table should be in, I purposefully added the 00 format but the double new-line was originally only there because I didn't know how to format the text when creating the post!

## Ruby, 49 chars

``1.upto(9){|n|puts"%02d "*9%(n..81).step(n).to_a}  ``

PS. With Wolfram Alpha it's 23 characters DS.

### Solution:2

Common Lisp, 79 characters (including whitespace):

``(dotimes (i 9) (dotimes (j 9) (format t "~2,'0d " (* (1+ i) (1+ j)))) (terpri))  ``

With something approaching a more traditional formatting:

``(dotimes (i 9)     (dotimes (j 9)       (format t "~2,'0d " (* (1+ i) (1+ j))))     (terpri))  ``

Then there's a 106-character version showing off the power of the formatter:

``(format t "~{~{~2,'0d~^ ~}~%~}"          (loop for i from 1 to 9              collect (loop for j from 1 to 9                         collect (* i j))))  ``

## PHP, 67 characters

``while(++\$x<10){\$y=0;while(++\$y<10)printf("%02d ",\$x*\$y);print"\n";}  ``

## C# using aggregate, 118 characters

``var r=Enumerable.Range(1,9).ToList();  r.ForEach(i=>Console.WriteLine(r.Aggregate("",(a,b)=>a+=(i*b).ToString("00 "))));  ``

## Bash, 59 chars

``for i in `seq 9`;do seq -w \$i \$i 99|sed 9q;done|column -c80  01  02  03  04  05  06  07  08  09  02  04  06  08  10  12  14  16  18  03  06  09  12  15  18  21  24  27  04  08  12  16  20  24  28  32  36  05  10  15  20  25  30  35  40  45  06  12  18  24  30  36  42  48  54  07  14  21  28  35  42  49  56  63  08  16  24  32  40  48  56  64  72  09  18  27  36  45  54  63  72  81  ``

### Solution:6

Haskell (not list comprehensions) 71 after import

It's a shame haskell doesn't have printf in it's prelude library but after importing in ghci 71 chars,the other haskell program was using list comprehensions:

``Prelude> :module Text.Printf  Prelude Text.Printf> let r=[1..9]  Prelude Text.Printf> mapM_(\y->(mapM_(\x->printf"%02d "(x*y))r)>>(putStrLn ""))r  01 02 03 04 05 06 07 08 09  02 04 06 08 10 12 14 16 18  03 06 09 12 15 18 21 24 27  04 08 12 16 20 24 28 32 36  05 10 15 20 25 30 35 40 45  06 12 18 24 30 36 42 48 54  07 14 21 28 35 42 49 56 63  08 16 24 32 40 48 56 64 72  09 18 27 36 45 54 63 72 81  ``

### Solution:7

JavaScript - with console - 65 chars

``for(i=0,a='';++i<10;a+='\n')for(j=0;++j<10;a+=i*j);console.log(a)  ``

JavaScript - with html - 68 chars

``for(i=0,a='';++i<10;a+='\n')for(j=0;++j<10;a+=i*j);document.write(a)  ``

## Repent, 11, 10 chars

``â†"1â‡„.â‡„^â†"â†»*;  ``

This is using my own toy language stack-based language Repent (which I will release soon, once it is up to scratch). I am sad to see J beating it, because I designed it only to beat J, Perl and Golfscript at code golf.

``V_1v.v^â†"â†»*;  ``

Interpreter (Alpha)

Language Reference (Alpha)

### Solution:9

C++ RANKING IN AT 99999TH PLACE (76 chars)

``for(int i=1;i<=9;++i){cout<<i<<" "<<i+1<<i+2<<i+3<<i+4<<i+5<<i+6<<i+7<<i+8;}  ``

### Solution:10

BASH 53

``for((i=1;i<10;i++));do seq -s' ' \$i \$i \$((9*i));done  ``

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »