Page 255 - Computer Science Class 11 With Functions
P. 255

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 function that takes upperLimit as an argument and prints the multiplication tables. The
                                            th          th
            multiplication table has 10 rows. The i  row has an i  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 10.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 6). For details, please see program 10.10.


            Program 10.10 Write a function multTables(upperLimit) to print the multiplication tables up to upper limit.
              01 def multTables(upperLimit):
              02     print('Multiplication Tables for ', 1, 'to', upperLimit)
              03     for i in range(1, 11):
              04         for col in range(1, upperLimit+1):
              05             print('{0:3}'.format(i*col), end=' ')
              06         print()
              07 multTables(15)


                    Make use of Program 10.10 in a program 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









                                                                                              Looping in Python  253
   250   251   252   253   254   255   256   257   258   259   260