Page 160 - Information_Practice_Fliipbook_Class11
P. 160

Sample Output:

         >>> Enter the number of rows: 6
         >>> Enter Symbol: *
              ***********
               *********
                *******
                 *****
                  ***
                   *
        Next, let us write a program to print an isosceles triangle using a given symbol (say, symbol). Program 6.14 shows
                                                                                  th
        an isosceles triangle comprising six rows using asterisks. Note that the last row (6  row) does not have any leading
                                               th
        spaces, 5  row has one leading space, the 4  row has two leading spaces, and so on. Finally, the first row has 5 (=6-1)
                th
                      th
        spaces. Thus, i  row has nRows-i leading spaces. Furthermore, the first row has one asterisk, the second row has
        3 asterisks, and so on. Finally, the sixth row has 11 (=6×2-1) asterisks. Thus, i  row has 2×i-1 asterisks. Now we are
                                                                            th
        ready to write a program to display an isosceles triangle.
         Program 6.14 To display the isosceles triangular pattern of a symbol

          01 '''
          02 Objective: To display an isosceles triangular pattern
          03 Inputs:
          04     nRows : number of rows
          05     symbol: symbol to be printed
          06 Output: Pattern of symbols
          07 '''
          08
          09 nRows = int(input('Enter the number of rows: '))
          10 symbol = input('Enter Symbol: ')
          11
          12 nSpaces = nRows-1
          13 nSymbols = 1
          14
          15 for row in range(1, nRows + 1):
          16     print(' ' * nSpaces, end = '')
          17     print(symbol * nSymbols)
          18     nSpaces = nSpaces - 1
          19     nSymbols = nSymbols + 2
        Sample Output:

         >>> Enter the number of rows: 6
         >>> Enter Symbol: *
                   *
                  ***
                 *****
                *******
               *********
              ***********
        6.7.3 Hollow Diamond

        Now it is time to apply our learning to draw a hollow diamond pattern. Sample Output of the given program shows a
        hollow diamond comprising 11 rows (six rows in a hollow upper isosceles triangle and five rows in an inverted hollow
        isosceles triangle). To build a hollow upper isosceles triangle, nUpper  is set to 6. In the first row, we need 5 (=6-1)
        spaces followed by one asterisk. For the next row, we need 4 (=6-2) leading spaces. The leading spaces are followed
        by two asterisks separated by one space, in all 3 (=2×2-1)characters. For the next row, we need 3(=6-3) leading spaces.
        The leading spaces are followed by two asterisks separated by three spaces, in all and 5 (=2×3-1) characters. Thus,


          146  Touchpad Informatics Practices-XI
   155   156   157   158   159   160   161   162   163   164   165