Page 224 - Computer Science Class 11 Without Functions
P. 224
Finally, the last row (6th row) has 5 (=6-1) spaces. Thus, beginning with zero leading spaces in the first row, the number
of leading spaces (say, nSpaces) increases by one in each following row. Furthermore, the first row has 11 (=6×2-1)
asterisks, the second row has 9 asterisks, and so on. Finally, the sixth row has one asterisk. Thus, beginning with 2 ×
nRows-1 asterisks in the first row, the number of asterisks (say, nSymbols) decreases by two in each following row.
We incorporate these details into the following program.
Program 9.13 To display the inverted isosceles triangular pattern of a symbol
01 '''
02 Objective: To display the right triangular pattern of a symbol
03 Inputs:
04 nRows : number of rows
05 symbol: symbol to be printed
06 Output: Pattern
07 '''
08 #Approach: In each line,
09 # first print enough blanks, then enough symbols
10 nRows = int(input('Enter the number of rows: '))
11 symbol = input('Enter Symbol: ')
12 nSpaces = 0
13 nSymbols = 2 * nRows - 1
14 for i in range(nRows):
15 print(' ' * nSpaces, end = '')
16 print(symbol * nSymbols)
17 nSpaces = nSpaces + 1
18 nSymbols = nSymbols - 2
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). Fig 9.8 shows an isosceles
th
triangle comprising six rows using asterisks. Note that the last row (6 row) does not have any leading spaces, 5 row
th
th
has one leading space, the 4 row has two leading spaces, and so on. Finally, the first row has 5 (=6-1) spaces. Thus,
th
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 ready to write
th
a program to display an isosceles triangle.
Program 9.14 To display the isosceles triangular pattern of a symbol
01 '''
02 Objective: To display an inverted isosceles triangular pattern
03 Inputs:
04 nRows : number of rows
05 symbol: symbol to be printed
06 Output: Inverted Pattern of symbols
07 '''
08
09 nRows = int(input('Enter the number of rows: '))
10 symbol = input('Enter Symbol: ')
11
222 Touchpad Computer Science-XI

