Anonymous user
Multiplication tables: Difference between revisions
m
→[[Print_a_Multiplication_Table#ALGOL 68]]: generalised table size
(Add C - Paddy always beats me to adding Python) |
m (→[[Print_a_Multiplication_Table#ALGOL 68]]: generalised table size) |
||
Line 7:
<lang Algol68>main:(
INT max = 12;
INT width = ENTIER(log(max)*2)+1;
STRING empty = " "*width, sep="|", hr = "+" + (max+1)*(width*"-"+"+");
FORMAT ifmt = $g(-width)"|"$; # remove leading zeros #
print(sep + IF width<2 THEN "x" ELSE " "*(width-2)+"x " FI + sep);
FOR col TO max DO printf((
FOR row TO max DO
▲ printf((fmt,row));
[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;
)</lang>
Output:
<pre>
+---+---+---+---+---+---+---+---+---+---+---+---+---+
x 1 2 3 4 5 6 7 8 9 10 11 12▼
| x
+---+---+---+---+---+---+---+---+---+---+---+---+---+
2 4 6 8 10 12 14 16 18 20 22 24▼
| 1|
|
| 3|
|
| 5|
| 6|
| 7|
|
|
| 10|
+---+---+---+---+---+---+---+---+---+---+---+---+---+
</pre>
|