Page 264 - CTS - CSA TP - Volume 2
P. 264
COMPUTER SOFTWARE APPLICATION - CITS
TASK 4 : Right-angled pattern with characters
Code:
print(“The character pattern using the ascii value is: “)
asciiValue = 65 # here, we are giving the ASCII value of A
foriin range(0, 5): # here, we are declaring the for loop for I values
for j in range(0, i + 1): # here, we are declaring the for loop for j values
# Here, the below will convert the ASCII value to the character
alphabate = chr(asciiValue)
print(alphabate, end=’ ‘) # Here, we are printing the alphabets
asciiValue += 1
# Here, we are incrementing the asciivalue by 1 after each iteration
print()
Explanation:
• The outer loop (for i in range(0, 5)) controls the number of rows in the pattern (5 rows in this case).
• The inner loop (for j in range(0, i + 1)) controls the number of characters in each row, and it increases with each
iteration of the outer loop.
• chr(asciiValue) converts the current ASCII value to the corresponding character.
• print(alphabate, end=’ ‘) prints the character without moving to the next line.
• asciiValue += 1 increments the ASCII value for the next character in the sequence.
• print() moves to the next line for the next row in the pattern.
Output:
249
CITS : IT & ITES - Computer Software Application - Exercise 126