Skip to content Skip to sidebar Skip to footer

Properly Formatted Multiplication Table

How would I make a multiplication table that's organized into a neat table? My current code is: n=int(input('Please enter a positive integer between 1 and 15: ')) for row in range(

Solution 1:

Quick way (Probably too much horizontal space though):

n=int(input('Please enter a positive integer between 1 and 15: '))
for row inrange(1,n+1):
    for col inrange(1,n+1):
        print(row*col, end="\t")
    print()

Better way:

n=int(input('Please enter a positive integer between 1 and 15: '))
for row inrange(1,n+1):
    print(*("{:3}".format(row*col) for col inrange(1, n+1)))

And using f-strings (Python3.6+)

for row inrange(1, n + 1):
    print(*(f"{row*col:3}"for col inrange(1, n + 1)))

Solution 2:

Gnibbler's approach is quite elegant. I went for the approach of constructing a list of list of integers first, using the range function and taking advantage of the step argument.

for n = 12

import pprint
n = 12
m = list(list(range(1*i,(n+1)*i, i)) for i in range(1,n+1))
pprint.pprint(m)
[[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, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],
 [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],
 [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],
 [6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],
 [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],
 [8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],
 [9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],
 [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
 [11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],
 [12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]]

Now that we have a list of list of integers that is in the form that we want, we should convert them into strings that are right justified with a width of one larger than the largest integer in the list of lists (the last integer), using the default argument of ' ' for the fillchar.

max_width = len(str(m[-1][-1])) + 1for i in m:
    i = [str(j).rjust(max_width) for j in i]
    print(''.join(i))

   123456789101112246810121416182022243691215182124273033364812162024283236404448510152025303540455055606121824303642485460667271421283542495663707784816243240485664728088969182736455463728190991081020304050607080901001101201122334455667788991101211321224364860728496108120132144

and demonstrate the elasticity of the spacing with a different size, e.g. n = 9

n=9
m = list(list(range(1*i,(n+1)*i, i)) for i inrange(1,n+1))
for i in m:
    i = [str(j).rjust(len(str(m[-1][-1]))+1) for j in i]
    print(''.join(i))

  1234567892468101214161836912151821242748121620242832365101520253035404561218243036424854714212835424956638162432404856647291827364554637281

Solution 3:

for i in range(1, 10) :
    for j in range(1, 10):
        print(repr(i*j).rjust(4),end=" ")
print()
print()

Output:

  1    2    3    4    5    6    7    8    9 

  2    4    6    8   10   12   14   16   18 

  3    6    9   12   15   18   21   24   27    

  4    8   12   16   20   24   28   32   36

  5   10   15   20   25   30   35   40   45 

  6   12   18   24   30   36   42   48   54 

  7   14   21   28   35   42   49   56   63 

  8   16   24   32   40   48   56   64   72 

  9   18   27   36   45   54   63   72   81 

or this one

for i in range(1, 11):
    for j in range(1, 11):
        print(("{:6d}".format(i * j,)), end='')
print()

the result is :

 1     2     3     4     5     6     7     8     9    10
 2     4     6     8    10    12    14    16    18    20
 3     6     9    12    15    18    21    24    27    30
 4     8    12    16    20    24    28    32    36    40
 5    10    15    20    25    30    35    40    45    50
 6    12    18    24    30    36    42    48    54    60
 7    14    21    28    35    42    49    56    63    70
 8    16    24    32    40    48    56    64    72    80
 9    18    27    36    45    54    63    72    81    90
10    20    30    40    50    60    70    80    90   100

Solution 4:

Or you could just do this (not as simplistic as the others but it works):

defmain():

    rows = int(input("Enter the number of rows that you would like to create a multiplication table for: "))
    counter = 0
    multiplicationTable(rows,counter)

defmultiplicationTable(rows,counter):

    size = rows + 1for i inrange (1,size):
        for nums inrange (1,size):
            value = i*nums
            print(value,sep=' ',end="\t")
            counter += 1if counter%rows == 0:
                print()
            else:
                counter
main()

Solution 5:

this one looks pretty neat:

print'\t\t\t======================================='print("\t\t\t\tMultiplication Tables")
   print'\t\t\t=======================================\n'for i in range(1,11):
       print'\t', i,
   printprint("___________________________________________________________________________________________________________________")

   for j in range(1,11):
       print("\n")
       print j, '|',
       for k in range(1,11):
           print'\t', j * k,
   print("\n")

Post a Comment for "Properly Formatted Multiplication Table"