In this code, we are going to print A BB CCC DDD pattern in Python.
The ASCII table for A, B, C… and a, b, c…
A 65 a 97
B 66 b 98
C 67 c 99
and so on
The logic of the code is:
- The inner loop moves horizontally, whereas the outer loop moves vertically.
- The outer loop will iterate from i = 0 to i = n
- The inner loop will iterate from j = 0 to j = i + 1.
- The inner loop will print the character value of the ASCII code in every iteration.
- As soon as the inner loop finishes executing the cursor will move to the next line and the value of ASCII will increase by 1.
Code for uppercase character.
n = int(input("Enter number of rows: "))
ascii = 65
for i in range(n):
for j in range(i + 1):
a = chr(ascii)
print(a, end = "")
ascii = ascii + 1
print("\n")
The output of the code is
Enter number of rows: 4
A
BB
CCC
DDDD
Enter number of rows: 7
A
BB
CCC
DDDD
EEEEE
FFFFFF
GGGGGGG
Code for lowercase character
n = int(input("Enter number of rows: "))
ascii = 97
for i in range(n):
for j in range(i + 1):
a = chr(ascii)
print(a, end = "")
ascii = ascii + 1
print("\n")
The output of the code is
Enter number of rows: 5
a
bb
ccc
dddd
eeeee
Enter number of rows: 7
a
bb
ccc
dddd
eeeee
ffffff
ggggggg