Multiplication tables: Difference between revisions

m
(Add C - Paddy always beats me to adding Python)
Line 7:
<lang Algol68>main:(
INT max = 12;
INT width = ENTIER(log(max)*2)+1;
STRING empty cell = " ";
STRING empty = " "*width, sep="|", hr = "+" + (max+1)*(width*"-"+"+");
FORMAT fmt = $zz-d$;
FORMAT ifmt = $g(-width)"|"$; # remove leading zeros #
 
printprintf(" ($gl$, x"hr));
print(sep + IF width<2 THEN "x" ELSE " "*(width-2)+"x " FI + sep);
FOR col TO max DO printf((fmtifmt, col)) OD;
print (new line);
printf((fmt$lgl$,row hr));
 
FOR row TO max DO
printf((fmt,row));
FOR col TO row-1 DO print(empty cell) OD;
[row:max]INT product;
FOR col FROM row TO max DO product[col]:=row*col OD;
STRING prefix=(empty+sep)*(row-1);
printf((fmt, product, $l$))
printf(($g$, sep, ifmt, row, $g$, prefix, ifmt, product, $l$))
OD;
printf((fmt$gl$, product, $l$hr))
)</lang>
Output:
<pre>
+---+---+---+---+---+---+---+---+---+---+---+---+---+
x 1 2 3 4 5 6 7 8 9 10 11 12
| x 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12|
+---+---+---+---+---+---+---+---+---+---+---+---+---+
2 4 6 8 10 12 14 16 18 20 22 24
| 1| 3 1| 2| 3| 4| 5| 96| 127| 158| 189| 10| 2111| 24 27 30 33 3612|
| 4 2| | 4| 6| 8| 10| 12| 14| 16| 18| 20| 22| 24 28 32 36 40 44 48|
| 3| 5 | | 9| 12| 15| 18| 21| 24| 25 27| 30| 33| 35 40 45 50 55 6036|
| 6 4| | | | 16| 20| 24| 28| 32| 36| 40| 42 44| 48 54 60 66 72|
| 5| 7 | | | | 25| 30| 35| 40| 45| 50| 55| 49 56 63 70 77 8460|
| 6| 8 | | | | | 36| 42| 48| 54| 60| 64 66| 72 80 88 96|
| 7| 9 | | | | | | 49| 56| 63| 70| 77| 81 90 99 10884|
| 108| | | | | | | | 64| 72| 80| 88| 100 110 12096|
| 119| | | | | | | | | 81| 90| 121 13299|108|
| 10| 12 | | | | | | | | 144|100|110|120|
| 11| 2 | | 4| 6| 8| 10 | 12 14| 16 | 18 20| 22 24|121|132|
 
| 12| x | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 11 12|144|
+---+---+---+---+---+---+---+---+---+---+---+---+---+
</pre>