Multiplication tables: Difference between revisions

From Rosetta Code
Content added Content deleted
(J: add some brief notes)
Line 970: Line 970:
└──┴────────────────────────────────────────────────┘</lang>
└──┴────────────────────────────────────────────────┘</lang>


That said, note that <code>*/~</code> is the core primitive used to construct a multiplication table and that <code>+/~</code> would make an addition. The rest is just to make it look pretty (and to blank out the lower triangle -- we use a greater than or equal table (<code><:/~</code>) to control that).
That said, note that <code>*/~</code> is the core primitive used to construct a multiplication table and this is a general technique so that, for example, <code>+/~</code> would make an addition table. The rest is just to make it look pretty (and to blank out the lower triangle -- we use a greater than or equal table (<code><:/~</code>) to control that).


=={{header|Java}}==
=={{header|Java}}==

Revision as of 12:33, 17 September 2010

Task
Multiplication tables
You are encouraged to solve this task according to the task description, using any language you may know.

Produce a formatted 12×12 multiplication table of the kind memorised by rote when in primary school.

Only print the top half triangle of products.

Ada

<lang Ada> with Ada.Text_IO; use Ada.Text_IO; with Ada.Strings.Fixed; use Ada.Strings.Fixed; procedure Multiplication_Table is

  package IO is new Integer_IO (Integer);
  use IO;

begin

  Put ("  | ");
  for Row in 1..12 loop
     Put (Row, Width => 4);
  end loop;
  New_Line;
  Put_Line ("--+-" & 12 * 4 * '-');
  for Row in 1..12 loop
     Put (Row, Width => 2);
     Put ("| ");
     for Column in 1..12 loop
        if Column < Row then
           Put ("    ");
        else
           Put (Row * Column, Width => 4);
        end if;
     end loop;
     New_Line;
  end loop;

end Multiplication_Table; </lang>

  |    1   2   3   4   5   6   7   8   9  10  11  12
--+-------------------------------------------------
 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
 3|            9  12  15  18  21  24  27  30  33  36
 4|               16  20  24  28  32  36  40  44  48
 5|                   25  30  35  40  45  50  55  60
 6|                       36  42  48  54  60  66  72
 7|                           49  56  63  70  77  84
 8|                               64  72  80  88  96
 9|                                   81  90  99 108
10|                                      100 110 120
11|                                          121 132
12|                                              144

ALGOL 68

Works with: ALGOL 68 version Standard - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny

<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 #
 printf(($gl$, hr));
 print(sep + IF width<2 THEN "x" ELSE " "*(width-2)+"x " FI + sep);
 FOR col TO max DO printf((ifmt, col)) OD;
 printf(($lgl$, hr));
 FOR row TO max DO
   [row:max]INT product;
   FOR col FROM row TO max DO product[col]:=row*col OD;
   STRING prefix=(empty+sep)*(row-1);
   printf(($g$, sep, ifmt, row, $g$, prefix, ifmt, product, $l$))
 OD;
 printf(($gl$, hr))

)</lang> Output:

+---+---+---+---+---+---+---+---+---+---+---+---+---+
| x |  1|  2|  3|  4|  5|  6|  7|  8|  9| 10| 11| 12|
+---+---+---+---+---+---+---+---+---+---+---+---+---+
|  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|
|  3|   |   |  9| 12| 15| 18| 21| 24| 27| 30| 33| 36|
|  4|   |   |   | 16| 20| 24| 28| 32| 36| 40| 44| 48|
|  5|   |   |   |   | 25| 30| 35| 40| 45| 50| 55| 60|
|  6|   |   |   |   |   | 36| 42| 48| 54| 60| 66| 72|
|  7|   |   |   |   |   |   | 49| 56| 63| 70| 77| 84|
|  8|   |   |   |   |   |   |   | 64| 72| 80| 88| 96|
|  9|   |   |   |   |   |   |   |   | 81| 90| 99|108|
| 10|   |   |   |   |   |   |   |   |   |100|110|120|
| 11|   |   |   |   |   |   |   |   |   |   |121|132|
| 12|   |   |   |   |   |   |   |   |   |   |   |144|
+---+---+---+---+---+---+---+---+---+---+---+---+---+

AppleScript

<lang AppleScript >set n to 12 -- Size of table. repeat with x from 0 to n if x = 0 then set {table, x} to {{return}, -1} repeat with y from 0 to n if y's contents = 0 then if x > 0 then set row to {f(x)} if x = -1 then set {row, x} to {{f("x")}, 1} else if y ≥ x then set end of row to f(x * y) if y < x then set end of row to f("") end if end repeat set end of table to row & return end repeat return table as string

-- Handler/Function for formatting fixed width integer string. on f(x) set text item delimiters to "" return (characters -4 thru -1 of (" " & x)) as string end f</lang>Output:

"
   x   1   2   3   4   5   6   7   8   9  10  11  12
   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
   3           9  12  15  18  21  24  27  30  33  36
   4              16  20  24  28  32  36  40  44  48
   5                  25  30  35  40  45  50  55  60
   6                      36  42  48  54  60  66  72
   7                          49  56  63  70  77  84
   8                              64  72  80  88  96
   9                                  81  90  99 108
  10                                     100 110 120
  11                                         121 132
  12                                             144
"

AutoHotkey

<lang autohotkey>Gui, -MinimizeBox Gui, Margin, 0, 0 Gui, Font, s9, Fixedsys Gui, Add, Edit, h0 w0 Gui, Add, Edit, w432 r14 -VScroll Gosub, Table Gui, Show,, Multiplication Table Return

GuiClose: GuiEscape:

   ExitApp

Return

Table:

   ; top row
   Table := "  x |"
   Loop, 12
       Table .= SubStr("   " A_Index, -3)
   Table .= "`n"
   ; underlines
   Table .= "----+"
   Loop, 48
       Table .= "-"
   Table .= "`n"
   ; table
   Loop, 12 { ; rows
       Table .= SubStr("  " Row := A_Index, -2) " |"
       Loop, 12 ; columns
           Table .= SubStr("    " (A_Index >= Row ? A_Index * Row : ""), -3)
       Table .= "`n"
   }
   GuiControl,, Edit2, %Table%

Return</lang> Message box shows:

  x |   1   2   3   4   5   6   7   8   9  10  11  12
----+------------------------------------------------
  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
  3 |           9  12  15  18  21  24  27  30  33  36
  4 |              16  20  24  28  32  36  40  44  48
  5 |                  25  30  35  40  45  50  55  60
  6 |                      36  42  48  54  60  66  72
  7 |                          49  56  63  70  77  84
  8 |                              64  72  80  88  96
  9 |                                  81  90  99 108
 10 |                                     100 110 120
 11 |                                         121 132
 12 |                                             144

C

<lang c>#include <math.h>

  1. include <stdio.h>

int main(int argc, char *argv[]) {

   int max = 12;
   const char *format = " %*d";
   const char *format2 = "%*s%c";
   int	dgts;
   int i,j;

   dgts = (int)(.99+ log10(1.0*max*max));
   
   printf(format2, dgts, "",'x');
   for (i=1; i <= max; i++) printf(format, dgts, i);
   printf("\n\n");
   
   for (j=1; j<=max; j++) {
     printf(format, dgts, j);
     for(i=1; i<j; i++) printf(format2, dgts, "",' ');
     for(i=j; i<=max; i++) printf(format, dgts, i*j);
     printf("\n");
   }
   printf("\n");
   return 0;

}</lang> Output:

   x   1   2   3   4   5   6   7   8   9  10  11  12

   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
   3           9  12  15  18  21  24  27  30  33  36
   4              16  20  24  28  32  36  40  44  48
   5                  25  30  35  40  45  50  55  60
   6                      36  42  48  54  60  66  72
   7                          49  56  63  70  77  84
   8                              64  72  80  88  96
   9                                  81  90  99 108
  10                                     100 110 120
  11                                         121 132
  12                                             144

C++

This is a slightly more-generalized version that takes any minimum and maximum table value, and formats the table columns.

<lang cpp>#include <iostream>

  1. include <iomanip>
  2. include <cmath> // for log10()
  3. include <algorithm> // for max()

size_t get_table_column_width(const int min, const int max) {

   unsigned int abs_max = std::max(max*max, min*min);
   // abs_max is the largest absolute value we might see.
   // If we take the log10 and add one, we get the string width
   // of the largest possible absolute value.
   // Add one for a little whitespace guarantee.
   size_t colwidth = 1 + std::log10(abs_max) + 1;
   // If only one of them is less than 0, then some will
   // be negative.
   bool has_negative_result = (min < 0) && (max > 0);
   // If some values may be negative, then we need to add some space
   // for a sign indicator (-)
   if(has_negative_result)
       colwidth++;
   return colwidth;

}

void print_table_header(const int min, const int max) {

   size_t colwidth = get_table_column_width(min, max);
   // table corner
   std::cout << std::setw(colwidth) << " ";
   
   for(int col = min; col <= max; ++col)
   {
       std::cout << std::setw(colwidth) << col;
   }
   // End header with a newline and blank line.
   std::cout << std::endl << std::endl;

}

void print_table_row(const int num, const int min, const int max) {

   size_t colwidth = get_table_column_width(min, max);
   // Header column
   std::cout << std::setw(colwidth) << num;
   // Spacing to ensure only the top half is printed
   for(int multiplicand = min; multiplicand < num; ++multiplicand)
   {
       std::cout << std::setw(colwidth) << " ";
   }
   // Remaining multiplicands for the row.
   for(int multiplicand = num; multiplicand <= max; ++multiplicand)
   {
       std::cout << std::setw(colwidth) << num * multiplicand;
   }
   // End row with a newline and blank line.
   std::cout << std::endl << std::endl;

}

void print_table(const int min, const int max) {

   // Header row
   print_table_header(min, max);
   // Table body
   for(int row = min; row <= max; ++row)
   {
       print_table_row(row, min, max);
   }

}

int main() {

   print_table(1, 12);
   return 0;

} </lang>

Output:

       1   2   3   4   5   6   7   8   9  10  11  12

   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

   3           9  12  15  18  21  24  27  30  33  36

   4              16  20  24  28  32  36  40  44  48

   5                  25  30  35  40  45  50  55  60

   6                      36  42  48  54  60  66  72

   7                          49  56  63  70  77  84

   8                              64  72  80  88  96

   9                                  81  90  99 108

  10                                     100 110 120

  11                                         121 132

  12                                             144

C#

<lang csharp>using System;

namespace multtbl {

   class Program
   {
       static void Main(string[] args)
       {
           Console.Write(" X".PadRight(4));
           for (byte i = 1; i <= 12; i++)
           {
               Console.Write(i.ToString("####").PadLeft(4));
           }
           Console.WriteLine();
           Console.Write(" ___");
           
           for (byte i = 1; i <= 12; i++)
           {
               Console.Write( " ___");
           }
           Console.WriteLine();
           for (byte row = 1; row <= 12; row++)
           {
               Console.Write(row.ToString("###").PadLeft(3).PadRight(4));
               for (byte col = 1; col <= 12; col++)
               {
                   if (row <= col)
                   {
                       Console.Write((row * col).ToString("###").PadLeft(4));
                   }
                   else
                   {
                       Console.Write("".PadLeft(4));
                   }
               }
               Console.WriteLine();
           }
           Console.WriteLine();
           Console.ReadLine();
       }
   }

} </lang>

Output:

 X     1   2   3   4   5   6   7   8   9  10  11  12
 ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___
  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
  3            9  12  15  18  21  24  27  30  33  36
  4               16  20  24  28  32  36  40  44  48
  5                   25  30  35  40  45  50  55  60
  6                       36  42  48  54  60  66  72
  7                           49  56  63  70  77  84
  8                               64  72  80  88  96
  9                                   81  90  99 108
 10                                      100 110 120
 11                                          121 132
 12                                              144

Chef

<lang chef>Multigrain Bread.

Prints out a multiplication table.

Ingredients. 12 cups flour 12 cups grains 12 cups seeds 1 cup water 9 dashes yeast 1 cup nuts 40 ml honey 1 cup sugar

Method. Sift the flour.

Put flour into the 1st mixing bowl.
Put yeast into the 1st mixing bowl.

Shake the flour until sifted. Put grains into the 2nd mixing bowl. Fold flour into the 2nd mixing bowl. Put water into the 2nd mixing bowl. Add yeast into the 2nd mixing bowl. Combine flour into the 2nd mixing bowl. Fold nuts into the 2nd mixing bowl. Liquify nuts. Put nuts into the 1st mixing bowl. Pour contents of the 1st mixing bowl into the baking dish. Sieve the flour.

Put yeast into the 2nd mixing bowl.
Add water into the 2nd mixing bowl.
Sprinkle the seeds.
  Put flour into the 2nd mixing bowl.
  Combine seeds into the 2nd mixing bowl.
  Put yeast into the 2nd mixing bowl.
  Put seeds into the 2nd mixing bowl.
  Remove flour from the 2nd mixing bowl.
  Fold honey into the 2nd mixing bowl.
  Put water into the 2nd mixing bowl.
  Fold sugar into the 2nd mixing bowl.
  Squeeze the honey.
    Put water into the 2nd mixing bowl.
    Remove water from the 2nd mixing bowl.
    Fold sugar into the 2nd mixing bowl.
    Set aside.
  Drip until squeezed.
  Scoop the sugar.
    Crush the seeds.
      Put yeast into the 2nd mixing bowl.
    Grind the seeds until crushed.
    Put water into the 2nd mixing bowl.
    Fold seeds into the 2nd mixing bowl.
    Set aside.
  Drop until scooped.
Randomize the seeds until sprinkled.
Fold honey into the 2nd mixing bowl.
Put flour into the 2nd mixing bowl.
Put grains into the 2nd mixing bowl.
Fold seeds into the 2nd mixing bowl.

Shake the flour until sieved. Put yeast into the 2nd mixing bowl. Add water into the 2nd mixing bowl. Pour contents of the 2nd mixing bowl into the 2nd baking dish.

Serves 2.</lang>

Output:

  x    1   2   3   4   5   6   7   8   9  10  11  12
   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
   3           9  12  15  18  21  24  27  30  33  36
   4              16  20  24  28  32  36  40  44  48
   5                  25  30  35  40  45  50  55  60
   6                      36  42  48  54  60  66  72
   7                          49  56  63  70  77  84
   8                              64  72  80  88  96
   9                                  81  90  99 108
  10                                     100 110 120
  11                                         121 132
  12                                             144

Clojure

This is more generalized. Any size can be used and the table will be formatted appropriately. <lang lisp>(let [size 12

     trange (range 1 (inc size))
     fmt-width (+ (.length (str (* size size))) 1)
     fmt-str (partial format (str "%" fmt-width "s"))
     fmt-dec (partial format (str "% " fmt-width "d"))]
 (doseq [s (cons
            (apply str (fmt-str " ") (map #(fmt-dec %) trange))
            (for [i trange]
              (apply str (fmt-dec i) (map #(fmt-str (str %))
                                          (map #(if (>= % i) (* i %) " ")
                                               (for [j trange] j))))))]
   (println s)))

</lang>

Output:

       1   2   3   4   5   6   7   8   9  10  11  12
   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
   3           9  12  15  18  21  24  27  30  33  36
   4              16  20  24  28  32  36  40  44  48
   5                  25  30  35  40  45  50  55  60
   6                      36  42  48  54  60  66  72
   7                          49  56  63  70  77  84
   8                              64  72  80  88  96
   9                                  81  90  99 108
  10                                     100 110 120
  11                                         121 132
  12                                             144

Common Lisp

<lang lisp> (do ((m 0 (if (= 12 m) 0 (1+ m)))

    (n 0 (if (= 12 m) (1+ n) n)))
   ((= n 13))
 (if (zerop n)
     (case m
       (0 (format t "  *|"))
       (12 (format t "  12~&---+------------------------------------------------~&"))
       (otherwise
        (format t "~4,D" m)))
     (case m
       (0 (format t "~3,D|" n))
       (12 (format t "~4,D~&" (* n m)))
       (otherwise
        (if (>= m n)
            (format t "~4,D" (* m n))
            (format t "    "))))))

</lang>

D

Note that the code could use some cleaning. Just like the C++ example, this too is in generalized form.

It uses C's printf to reduce the number of imported modules. In order to use std.stdio.writefln, all "%.*s" should be converted to "%s" (but NOT "%*d"). <lang d>import std.string;

void printTable(uint minsize, uint maxsize) {

   uint maxlen = toString(maxsize * maxsize).length;
   uint x, y;
   char[] hline = new char[](maxlen);
   hline[] = '-';
   char[] hspace = new char[](maxlen);
   hspace[] = ' ';
   for(x = minsize; x <= maxsize+1; x++)
       printf("+%.*s", hline);
   printf("+\n|%.*sx%.*s", hspace[0..$/2], ((hspace.length % 2) == 1) ? hspace[0..$/2] : hspace[0..$/2-1]);
   for(x = minsize; x <= maxsize; x++)
       printf("|%*d", maxlen, x);
   printf("|\n");
   for(x = minsize; x <= maxsize+1; x++)
       printf("+%.*s", hline);
   printf("+\n");
   for(y = minsize; y <= maxsize; y++)
   {
       printf("|%*d", maxlen, y);
       for(x = minsize; x <= maxsize; x++)
       {
           if(x >= y)
               printf("|%*d", maxlen, x * y);
           else
               printf("|%.*s", hspace);
       }
       printf("|\n");
   }
   for(x = minsize; x <= maxsize+1; x++)
       printf("+%.*s", hline);
   printf("+\n");

}

void main() {

   printTable(1, 12);

}</lang>

Output:

+---+---+---+---+---+---+---+---+---+---+---+---+---+
| x |  1|  2|  3|  4|  5|  6|  7|  8|  9| 10| 11| 12|
+---+---+---+---+---+---+---+---+---+---+---+---+---+
|  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|
|  3|   |   |  9| 12| 15| 18| 21| 24| 27| 30| 33| 36|
|  4|   |   |   | 16| 20| 24| 28| 32| 36| 40| 44| 48|
|  5|   |   |   |   | 25| 30| 35| 40| 45| 50| 55| 60|
|  6|   |   |   |   |   | 36| 42| 48| 54| 60| 66| 72|
|  7|   |   |   |   |   |   | 49| 56| 63| 70| 77| 84|
|  8|   |   |   |   |   |   |   | 64| 72| 80| 88| 96|
|  9|   |   |   |   |   |   |   |   | 81| 90| 99|108|
| 10|   |   |   |   |   |   |   |   |   |100|110|120|
| 11|   |   |   |   |   |   |   |   |   |   |121|132|
| 12|   |   |   |   |   |   |   |   |   |   |   |144|
+---+---+---+---+---+---+---+---+---+---+---+---+---+

E

<lang e> def size := 12

 println(`{|style="border-collapse: collapse; text-align: right;"`)
 println(`|`)
 for x in 1..size {
   println(`|style="border-bottom: 1px solid black; " | $x`)
 }
 for y in 1..size {
   println(`|-`)
     println(`|style="border-right: 1px solid black;" | $y`)
   for x in 1..size {
     println(`|  ${if (x >= y) { x*y } else {""}}`)
   }
 }
 println("|}")</lang>

Targets MediaWiki markup. Output:

1 2 3 4 5 6 7 8 9 10 11 12
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
3      9  12  15  18  21  24  27  30  33  36
4        16  20  24  28  32  36  40  44  48
5          25  30  35  40  45  50  55  60
6            36  42  48  54  60  66  72
7              49  56  63  70  77  84
8                64  72  80  88  96
9                  81  90  99  108
10                    100  110  120
11                      121  132
12                        144

Factor

<lang factor>USING: io kernel math math.parser math.ranges sequences ; IN: multiplication-table

print-row ( n -- )
   [ number>string 2 CHAR: space pad-head write " |" write ]
   [ 1 - [ "    " write ] times ]
   [
       dup 12 [a,b]
       [ * number>string 4 CHAR: space pad-head write ] with each
   ] tri nl ;
print-table ( -- )
   "    " write
   1 12 [a,b] [ number>string 4 CHAR: space pad-head write ] each nl
   "   +" write
   12 [ "----" write ] times nl
   1 12 [a,b] [ print-row ] each ;</lang>
       1   2   3   4   5   6   7   8   9  10  11  12
   +------------------------------------------------
 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
 3 |           9  12  15  18  21  24  27  30  33  36
 4 |              16  20  24  28  32  36  40  44  48
 5 |                  25  30  35  40  45  50  55  60
 6 |                      36  42  48  54  60  66  72
 7 |                          49  56  63  70  77  84
 8 |                              64  72  80  88  96
 9 |                                  81  90  99 108
10 |                                     100 110 120
11 |                                         121 132
12 |                                             144

FALSE

<lang false>[$100\>[" "]?$10\>[" "]?." "]p: [$p;! m: 2[$m;\>][" "1+]# [$13\>][$m;*p;!1+]#%" "]l: 1[$13\>][$l;!1+]#%</lang>

Forth

<lang forth>

multiplication-table
 cr 2 spaces  13 2 do i 4 u.r loop
 cr
 13 2 do
   cr i 2 u.r
   13 2 do
     i j < if 4 spaces else i j * 4 u.r then
   loop
 loop ;

</lang>

Fortran

Works with: Fortran version 90 and later

<lang fortran>program multtable implicit none

 integer :: i, j, k
   write(*, "(a)") " x|   1   2   3   4   5   6   7   8   9  10  11  12"
   write(*, "(a)") "--+------------------------------------------------"
   do i = 1, 12
     write(*, "(i2, a)", advance="no") i, "|"

do k = 2, i

   	  write(*, "(a4)", advance="no") ""
       end do
   	do j = i, 12
         write(*, "(i4)", advance="no") i*j
       end do
       write(*, *)
   end do

end program multtable</lang>

Haskell

<lang haskell>import Control.Monad import Text.Printf

main = do

   putStrLn $ "   x" ++ concatMap fmt [1..12]
   zipWithM_ f [1..12] $ iterate ("    " ++) ""
 where f n s = putStrLn $ fmt n ++ s ++ concatMap (fmt . (*n)) [n..12]
       fmt n = printf "%4d" (n :: Int)</lang>

HicEst

<lang HicEst>WRITE(Row=1) " x 1 2 3 4 5 6 7 8 9 10 11 12" DO line = 1, 12

 WRITE(Row=line+2, Format='i2') line
 DO col = line, 12
   WRITE(Row=line+2, Column=4*col, Format='i3') line*col
 ENDDO

ENDDO</lang>

Icon and Unicon

Icon

<lang Icon>procedure main() lim := 13 wid := 5 every writes(right("* |" | (1 to lim) | "\n",wid)|right("\n",wid*(lim+1),"_")) # header row and separator every (i := 1 to lim) &

  writes(right( i||" |" | (j := 1 to lim, if j < i then "" else i*j) | "\n",wid))     # table content and triangle

end </lang>

The above example is a somewhat exaggerated example of contractions. In both cases 'every' is used to force all alternatives including row labels, column headings, content, line terminators. The upper triangle is produced by embedding an 'if' expression inside the object of an 'every' (normally an error prone construct which would malfunction if not carefully separated from the generators for 'i' and 'j' - an all too tempting possibility once you get into this mind set.)


Sample output:

 * |    1    2    3    4    5    6    7    8    9   10   11   12   13    
_____________________________________________________________________
  1 |    1    2    3    4    5    6    7    8    9   10   11   12   13    
  2 |         4    6    8   10   12   14   16   18   20   22   24   26    
  3 |              9   12   15   18   21   24   27   30   33   36   39    
  4 |                  16   20   24   28   32   36   40   44   48   52    
  5 |                       25   30   35   40   45   50   55   60   65    
  6 |                            36   42   48   54   60   66   72   78    
  7 |                                 49   56   63   70   77   84   91    
  8 |                                      64   72   80   88   96  104    
  9 |                                           81   90   99  108  117    
 10 |                                               100  110  120  130    
 11 |                                                    121  132  143    
 12 |                                                         144  156    
 13 |                                                              169 

Unicon

The Icon solution works in Unicon.

J

<lang j> multtable=: <:/~ * */~

  format=: 'b4.0' 8!:2 ]
  (('*' ; ,.) ,. ({. ; ])@format@multtable) >:i.12

┌──┬────────────────────────────────────────────────┐ │* │ 1 2 3 4 5 6 7 8 9 10 11 12│ ├──┼────────────────────────────────────────────────┤ │ 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│ │ 3│ 9 12 15 18 21 24 27 30 33 36│ │ 4│ 16 20 24 28 32 36 40 44 48│ │ 5│ 25 30 35 40 45 50 55 60│ │ 6│ 36 42 48 54 60 66 72│ │ 7│ 49 56 63 70 77 84│ │ 8│ 64 72 80 88 96│ │ 9│ 81 90 99 108│ │10│ 100 110 120│ │11│ 121 132│ │12│ 144│ └──┴────────────────────────────────────────────────┘</lang>

That said, note that */~ is the core primitive used to construct a multiplication table and this is a general technique so that, for example, +/~ would make an addition table. The rest is just to make it look pretty (and to blank out the lower triangle -- we use a greater than or equal table (<:/~) to control that).

Java

<lang> public class MulTable{ public static void main(String args[]){ int i,j; for(i=1;i<=12;i++) { System.out.print("\t"+i); }

System.out.println(""); for(i=0;i<100;i++) System.out.print("-"); System.out.println(""); for(i=1;i<=12;i++){ System.out.print(""+i+"|"); for(j=1;j<=12;j++){ if(j<i) System.out.print("\t"); else System.out.print("\t"+i*j); } System.out.println(""); } } } </lang> Output: <lang>

       1       2       3       4       5       6       7       8       9       10      11      12

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 3| 9 12 15 18 21 24 27 30 33 36 4| 16 20 24 28 32 36 40 44 48 5| 25 30 35 40 45 50 55 60 6| 36 42 48 54 60 66 72 7| 49 56 63 70 77 84 8| 64 72 80 88 96 9| 81 90 99 108 10| 100 110 120 11| 121 132 12| 144 </lang>

JavaScript

<lang html4strict><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" > <title>12 times table</title> <script type='text/javascript'>

   function multiplication_table(n, target) {
       var table = document.createElement('table');
       var row = document.createElement('tr');
       var cell = document.createElement('th');
       cell.appendChild(document.createTextNode('x'));
       row.appendChild(cell);
       for (var x = 1; x <=n; x++) {
           cell = document.createElement('th');
           cell.appendChild(document.createTextNode(x));
           row.appendChild(cell);
       }
       table.appendChild(row);
       for (var x = 1; x <=n; x++) {
           row = document.createElement('tr');
           cell = document.createElement('th');
           cell.appendChild(document.createTextNode(x));
           row.appendChild(cell);
           var y;
           for (y = 1; y < x; y++) {
               cell = document.createElement('td');
               cell.appendChild(document.createTextNode('\u00a0'));
               row.appendChild(cell);
           }
           for (; y <= n; y++) {
               cell = document.createElement('td');
               cell.appendChild(document.createTextNode(x*y));
               row.appendChild(cell);
           }
           table.appendChild(row);
       }
       target.appendChild(table);
   }

</script> <style type='text/css'>

   body {font-family: sans-serif;}
   table {border-collapse: collapse;}
   th, td {border: 1px solid black; text-align: right; width: 4ex;}

</style> </head> <body onload="multiplication_table(12, document.getElementById('target'));">

</body> </html></lang>

Outputs (minus the style):

x123456789101112
1123456789101112
2 4681012141618202224
3 9121518212427303336
4 162024283236404448
5 2530354045505560
6 36424854606672
7 495663707784
8 6472808896
9 819099108
10 100110120
11 121132
12 144

Works with: UCB Logo

<lang logo>to mult.table :n

 type "|  | for [i 2 :n] [type form :i 4 0] (print)
 (print)
 for [i 2 :n] [
   type form :i 2 0
   for [j 2 :n] [
     type ifelse :i > :j ["|    |] [form :i*:j 4 0]
   ]
   (print)
 ]

end

mult.table 12 </lang>

MATLAB

timesTable.m: (creates Times Table of N degree)

<lang MATLAB>function table = timesTable(N)

   table = [(0:N); (1:N)' triu( kron((1:N),(1:N)') )];

end</lang>

A minimally vectorized version of the above code:

<lang MATLAB>function table = timesTable(N)

   %Generates a column vector with integers from 1 to N
   rowLabels = (1:N)';
   
   %Generate a row vector with integers from 0 to N
   columnLabels = (0:N);
   
   %Generate the multiplication table using the kronecker tensor product
   %of two vectors one a column vector and the other a row vector
   table = kron((1:N),(1:N)');
   
   %Make it upper triangular and concatenate the rowLabels and 
   %columnLabels to the table
   table = [columnLabels; rowLabels triu(table)];
     

end</lang>


For N=12 the output is: <lang MATLAB>timesTable(12)

ans =

    0     1     2     3     4     5     6     7     8     9    10    11    12
    1     1     2     3     4     5     6     7     8     9    10    11    12
    2     0     4     6     8    10    12    14    16    18    20    22    24
    3     0     0     9    12    15    18    21    24    27    30    33    36
    4     0     0     0    16    20    24    28    32    36    40    44    48
    5     0     0     0     0    25    30    35    40    45    50    55    60
    6     0     0     0     0     0    36    42    48    54    60    66    72
    7     0     0     0     0     0     0    49    56    63    70    77    84
    8     0     0     0     0     0     0     0    64    72    80    88    96
    9     0     0     0     0     0     0     0     0    81    90    99   108
   10     0     0     0     0     0     0     0     0     0   100   110   120
   11     0     0     0     0     0     0     0     0     0     0   121   132
   12     0     0     0     0     0     0     0     0     0     0     0   144

</lang>

MUMPS

<lang MUMPS>MULTTABLE(SIZE)

;Print out a multiplication table
;SIZE is the size of the multiplication table to make
;MW is the maximum width of the numbers
;D is the down axis
;A is the across axis
;BAR is the horizontal bar under the operands
NEW MW,D,A,BAR
IF $DATA(SIZE)<1 SET SIZE=12
SET MW=$LENGTH(SIZE*SIZE)
SET BAR="" FOR I=1:1:(MW+2) SET BAR=BAR_"-"
FOR D=1:1:(SIZE+2) DO
.FOR A=1:1:(SIZE+1) DO
..WRITE:(D=1)&(A=1) !,$JUSTIFY("",MW-1)," X|"
..WRITE:(D=1)&(A>1) ?((A-1)*5),$JUSTIFY((A-1),MW)
..WRITE:(D=2)&(A=1) !,BAR
..WRITE:(D=2)&(A'=1) BAR
..WRITE:(D>2)&(A=1) !,$JUSTIFY((D-2),MW)," |"
..WRITE:((A-1)>=(D-2))&((D-2)>=1) ?((A-1)*5),$JUSTIFY((D-2)*(A-1),MW)
KILL MW,D,A,BAR
QUIT</lang>

Output:

USER>D MULTTABLE^ROSETTA
 
   X|  1    2    3    4    5    6    7    8    9   10   11   12
-----------------------------------------------------------------
  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
  3 |            9   12   15   18   21   24   27   30   33   36
  4 |                16   20   24   28   32   36   40   44   48
  5 |                     25   30   35   40   45   50   55   60
  6 |                          36   42   48   54   60   66   72
  7 |                               49   56   63   70   77   84
  8 |                                    64   72   80   88   96
  9 |                                         81   90   99  108
 10 |                                             100  110  120
 11 |                                                  121  132
 12 |                                                       144

OCaml

Translation of: C

<lang ocaml>let () =

 let max = 12 in
 let fmax = float_of_int max in
 let dgts = int_of_float (ceil (log10 (fmax *. fmax))) in
 let fmt = Printf.printf " %*d" dgts in
 let fmt2 = Printf.printf "%*s%c" dgts in
 fmt2 "" 'x';
 for i = 1 to max do fmt i done;
 print_string "\n\n";
 for j = 1 to max do
   fmt j;
   for i = 1 to pred j do fmt2 "" ' '; done;
   for i = j to max do fmt (i*j); done;
   print_newline()
 done;
 print_newline()</lang>

Perl

<lang perl>our $max = 12; our $width = length($max**2) + 1;

printf "%*s", $width, $_ foreach 'x|', 1..$max; print "\n", '-' x ($width - 1), '+', '-' x ($max*$width), "\n"; foreach my $i (1..$max) { printf "%*s", $width, $_

           foreach "$i|", map { $_ >= $i and $_*$i } 1..$max;

print "\n"; }</lang>

Output:

  x|   1   2   3   4   5   6   7   8   9  10  11  12
---+------------------------------------------------
  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
  3|           9  12  15  18  21  24  27  30  33  36
  4|              16  20  24  28  32  36  40  44  48
  5|                  25  30  35  40  45  50  55  60
  6|                      36  42  48  54  60  66  72
  7|                          49  56  63  70  77  84
  8|                              64  72  80  88  96
  9|                                  81  90  99 108
 10|                                     100 110 120
 11|                                         121 132
 12|                                             144

Perl 6

Translation of: Perl
Works with: Rakudo Star version 2010.08

<lang perl6>my $max = 12; my $width = chars $max**2; my $f = "%{$width}s";

say 'x'.fmt($f), '┃ ', (1..$max).fmt($f); say '━' x $width, '╋', '━' x $max*$width + $max; for 1..$max -> $i {

   say $i.fmt($f), '┃ ', (
       for 1..$max -> $j {
           $i <= $j ?? $i*$j !! ;
       }
   ).fmt($f);

}</lang>

Output:

  x┃   1   2   3   4   5   6   7   8   9  10  11  12
━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  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
  3┃           9  12  15  18  21  24  27  30  33  36
  4┃              16  20  24  28  32  36  40  44  48
  5┃                  25  30  35  40  45  50  55  60
  6┃                      36  42  48  54  60  66  72
  7┃                          49  56  63  70  77  84
  8┃                              64  72  80  88  96
  9┃                                  81  90  99 108
 10┃                                     100 110 120
 11┃                                         121 132
 12┃                                             144

PL/I

<lang PL/I> /* 12 x 12 multiplication tanle. */

multiplication_table: procedure options (main);

  declare (i, j) fixed decimal (2);
  put skip edit ((i do i = 1 to 12)) (X(4), 12 F(4));
  put skip edit ( (49)'_') (X(3), A);
  do i = 1 to 12;
     put skip edit (i, ' |', (i*j do j = i to 12))
        (F(2), a, col(i*4+1), 12 F(4));
  end;

end multiplication_table; </lang>

Result:

<lang>

      1   2   3   4   5   6   7   8   9  10  11  12
  _________________________________________________
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
3 |           9  12  15  18  21  24  27  30  33  36
4 |              16  20  24  28  32  36  40  44  48
5 |                  25  30  35  40  45  50  55  60
6 |                      36  42  48  54  60  66  72
7 |                          49  56  63  70  77  84
8 |                              64  72  80  88  96
9 |                                  81  90  99 108

10 | 100 110 120 11 | 121 132 12 | 144 </lang>

PicoLisp

<lang PicoLisp>(de mulTable (N)

  (space 4)
  (for X N
     (prin (align 4 X)) )
  (prinl)
  (prinl)
  (for Y N
     (prin (align 4 Y))
     (space (* (dec Y) 4))
     (for (X Y (>= N X) (inc X))
        (prin (align 4 (* X Y))) )
     (prinl) ) )

(mulTable 12)</lang> Output:

       1   2   3   4   5   6   7   8   9  10  11  12

   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
   3           9  12  15  18  21  24  27  30  33  36
   4              16  20  24  28  32  36  40  44  48
   5                  25  30  35  40  45  50  55  60
   6                      36  42  48  54  60  66  72
   7                          49  56  63  70  77  84
   8                              64  72  80  88  96
   9                                  81  90  99 108
  10                                     100 110 120
  11                                         121 132
  12                                             144

PureBasic

<lang PureBasic>Procedure PrintMultiplicationTable(maxx, maxy)

 sp       = Len(Str(maxx*maxy)) + 1
 trenner$ =  "+"
 For l1 = 1 To maxx + 1
   For l2 = 1 To sp
     trenner$ + "-"
   Next
   trenner$ + "+"
 Next
 header$ = "|" + RSet("x", sp) + "|"
 For a = 1 To maxx
   header$ + RSet(Str(a), sp)
   header$ + "|"
 Next
 PrintN(trenner$) 
 PrintN(header$)
 PrintN(trenner$)
 For y = 1 To maxy
   line$ = "|" + RSet(Str(y), sp) + "|"
   For x = 1 To maxx
     If x >= y
       line$ + RSet(Str(x*y), sp)
     Else
       line$ + Space(sp)
     EndIf
     line$ + "|"
   Next
   PrintN(line$)
 Next
 PrintN(trenner$)

EndProcedure

OpenConsole() PrintMultiplicationTable(12, 12) Input()</lang>

Ouput similar to ALGOL 68

Python

<lang python>>>> size = 12 >>> width = len(str(size**2)) >>> for row in range(-1,size+1): if row==0: print("─"*width + "┼"+"─"*((width+1)*size-1)) else: print("".join("%*s%1s" % ((width,) + (("x","│") if row==-1 and col==0 else (row,"│") if row>0 and col==0 else (col,"") if row==-1 else ("","") if row>col else (row*col,""))) for col in range(size+1)))


 x│  1   2   3   4   5   6   7   8   9  10  11  12 

───┼───────────────────────────────────────────────

 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 
 3│          9  12  15  18  21  24  27  30  33  36 
 4│             16  20  24  28  32  36  40  44  48 
 5│                 25  30  35  40  45  50  55  60 
 6│                     36  42  48  54  60  66  72 
 7│                         49  56  63  70  77  84 
 8│                             64  72  80  88  96 
 9│                                 81  90  99 108 
10│                                    100 110 120 
11│                                        121 132 
12│                                            144 

>>> </lang>

The above works with Python 3.X, which uses Unicode strings by default.
Declaring a file type of UTF-8 and adding a u to all string literals to transform them into Unicode literals would make the above work in Python 2.X. (As would using ASCII minus, plus, and pipe characters: "-", "+", "|"; instead of the non-ASCII chars used to draw a frame).

R

<lang r> multiplication_table <- function(n=12) {

  one_to_n <- 1:n
  x <- matrix(one_to_n) %*% t(one_to_n)
  x[lower.tri(x)] <- 0
  rownames(x) <- colnames(x) <- one_to_n
  print(as.table(x), zero.print="")
  invisible(x)

} multiplication_table() </lang>

REBOL

<lang REBOL>REBOL [ Title: "12x12 Multiplication Table" Author: oofoe Date: 2009-12-26 URL: http://rosettacode.org/wiki/Print_a_Multiplication_Table ]

size: 12

Because of REBOL's GUI focus, it doesn't really do pictured output,
so I roll my own. See Formatted_Numeric_Output for more
comprehensive version

pad: func [pad n][

   n: to-string n
   insert/dup n " " (pad - length? n)
   n 

] p3: func [v][pad 3 v]  ; A shortcut, I hate to type...

--: has [x][repeat x size + 1 [prin "+---"] print "+"]  ; Special chars OK.

.row: func [label y /local row x][ row: reduce ["|" label "|"] repeat x size [append row reduce [either x < y [" "][p3 x * y] "|"]] print rejoin row ]

-- .row " x " 1 -- repeat y size [.row p3 y y] --

print rejoin [ crlf "What about " size: 5 "?" crlf ] -- .row " x " 1 -- repeat y size [.row p3 y y] --

print rejoin [ crlf "How about " size: 20 "?" crlf ] -- .row " x " 1 -- repeat y size [.row p3 y y] --</lang>

Output (only 12x12 shown):

+---+---+---+---+---+---+---+---+---+---+---+---+---+
| x |  1|  2|  3|  4|  5|  6|  7|  8|  9| 10| 11| 12|
+---+---+---+---+---+---+---+---+---+---+---+---+---+
|  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|
|  3|   |   |  9| 12| 15| 18| 21| 24| 27| 30| 33| 36|
|  4|   |   |   | 16| 20| 24| 28| 32| 36| 40| 44| 48|
|  5|   |   |   |   | 25| 30| 35| 40| 45| 50| 55| 60|
|  6|   |   |   |   |   | 36| 42| 48| 54| 60| 66| 72|
|  7|   |   |   |   |   |   | 49| 56| 63| 70| 77| 84|
|  8|   |   |   |   |   |   |   | 64| 72| 80| 88| 96|
|  9|   |   |   |   |   |   |   |   | 81| 90| 99|108|
| 10|   |   |   |   |   |   |   |   |   |100|110|120|
| 11|   |   |   |   |   |   |   |   |   |   |121|132|
| 12|   |   |   |   |   |   |   |   |   |   |   |144|
+---+---+---+---+---+---+---+---+---+---+---+---+---+

Ruby

<lang ruby>def multiplication_table(n)

 puts "    " + ((" %3d" * n) % (1..n).to_a)
 1.upto(n) do |x|
   print "%3d " % x
   1.upto(x-1) {|y| print "    "}
   x.upto(n)   {|y| print " %3d" % (x*y)}
   puts ""
 end

end

multiplication_table 12</lang>

Tcl

<lang tcl>puts " x\u2502 1 2 3 4 5 6 7 8 9 10 11 12" puts \u0020\u2500\u2500\u253c[string repeat \u2500 48] for {set i 1} {$i <= 12} {incr i} {

   puts -nonewline [format "%3d" $i]\u2502[string repeat " " [expr {$i*4-4}]]
   for {set j 1} {$j <= 12} {incr j} {

if {$j >= $i} { puts -nonewline [format "%4d" [expr {$i*$j}]] }

   }
   puts ""

}</lang> Output:

  x│   1   2   3   4   5   6   7   8   9  10  11  12
 ──┼────────────────────────────────────────────────
  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
  3│           9  12  15  18  21  24  27  30  33  36
  4│              16  20  24  28  32  36  40  44  48
  5│                  25  30  35  40  45  50  55  60
  6│                      36  42  48  54  60  66  72
  7│                          49  56  63  70  77  84
  8│                              64  72  80  88  96
  9│                                  81  90  99 108
 10│                                     100 110 120
 11│                                         121 132
 12│                                             144

Ursala

It's no more difficult to express the general case than the size 12 case, so a table generating function parameterized by the size is used. <lang Ursala>

  1. import std
  2. import nat

table "n" =

~&plrTS(

  ~&xS pad` @xS <'x  ','--'>-- --' | '*hS %nP* nrange/1 "n",
  ^CthPiC(`-!*h,~&) mat` *xSSK7 pad` *K7ihxPBSS (~&i&& %nP)** nleq&&product**iiK0lK2x nrange/1 "n")
  1. show+

main = table 12 </lang> A better way of using Ursala to make tables would be with the tbl library included with the standard package, which can generate LaTeX code for arbitrary heading hierarchies and typesetting options, but here it is in ASCII art.

  x  1 2 3  4  5  6  7  8  9  10  11  12
   -------------------------------------
 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
 3 |     9 12 15 18 21 24 27  30  33  36
 4 |       16 20 24 28 32 36  40  44  48
 5 |          25 30 35 40 45  50  55  60
 6 |             36 42 48 54  60  66  72
 7 |                49 56 63  70  77  84
 8 |                   64 72  80  88  96
 9 |                      81  90  99 108
10 |                         100 110 120
11 |                             121 132
12 |                                 144