Page 222 - Computer Science Class 11 Without Functions
P. 222

9.6.2 Multiplication Tables

        Students  in  elementary  classes  are  often  required  to  memorise  the  multiplication  tables.  Suppose  you  need  to
        help  your  sister  with  memorising  the  multiplication  tables.  To  help  her,  you  need  to  know  up  to  which  number
        (upperLimit) she needs the multiplication tables. For example, in the following table, the upper limit is 15.

          1   2   3   4   5   6   7   8   9   10   11   12   13   14   15
          2   4   6   8  10  12  14  16  18   20   22   24   26   28   30
          3   6   9  12  15  18  21  24  27   30   33   36   39   42   45
          4   8  12  16  20  24  28  32  36   40   44   48   52   56   60

          5  10  15  20  25  30  35  40  45   50   55   60   65   70   75
          6  12  18  24  30  36  42  48  54   60   66   72   78   84   90
          7  14  21  28  35  42  49  56  63   70   77   84   91   98  105
          8  16  24  32  40  48  56  64  72   80   88   96  104  112  120
          9  18  27  36  45  54  63  72  81   90   99  108  117  126  135
         10  20  30  40  50  60  70  80  90  100  110  120  130  140  150
        Now  let  us  develop  a  program  that  takes  upperLimit  as  an  input  and  prints  the  multiplication  tables.  The
        multiplication table has 10 rows. The ith row has an ith multiple of each number in the range range (upperLimit+1).
        So, the outer loop would run for each row, and the inner loop would run for column (col), and print i*col (see
        Program 9.10). To make the table nice looking, we print each number using three column positions and use the

        specification '{0:3}'.format(i*col) for printing. The statement print('{0:3}'.format(i * col),
        end=' ') prints the product of i and col formatted to take up at least 3 characters (including digits and spaces). The
        end=' ' parameter ensures that the values are separated by a space rather than a newline character. The numbers in
        each row are separated by a blank. Once a row is complete, we transfer the print control to the next line by executing
        a print statement (line 7). For details, please see Program 9.10.

         Program 9.10 To print multiplication tables

          01 upperLimit = int(input('Enter the upper limit: '))
          02
          03 print('Multiplication Tables for ', 1, 'to', upperLimit)
          04
          05 for i in range(1, 11):
          06     for col in range(1, upperLimit+1):
          07         print('{0:3}'.format(i*col), end=' ')
          08     print()

                Make appropriate changes in the Program 9.10 to print multiplication tables of numbers from lowerLimit  up
                to a upperLimit, provided as input by the user. For example, for the numbers 3 and 6 provided as input for
                lowerLimit and upperLimit, the program should yield the following output:

                Multiplication Tables for  3 to 6
                  3   4   5   6
                  6   8  10  12
                  9  12  15  18
                 12  16  20  24
                 15  20  25  30
                 18  24  30  36
                 21  28  35  42
                 24  32  40  48
                 27  36  45  54
                 30  40  50  60


         220   Touchpad Computer Science-XI
   217   218   219   220   221   222   223   224   225   226   227