What is the alphabet pattern?
Alphabet pattern is a series of alphabets (uppercase or lowercase) which creates a certain pattern or geometrical shape such as square, pyramid, triangle, etc. These patterns are created using nested controlled loops.
These pattern programs are used to practice and boost programming skills and also asked to create in programming interviews.
A A B A B C A B C D A B C D E
To create above pattern run 2 nested
for loop. Run external loop for 'N' number of times, where 'N' is number of rows in the square, i.e
for(let i = 0;i < N;i++).
The internal loop will run for 1 time in the first iteration of external code, 2 times in the second iteration, and so on.
In each iteration of internal loop add 65 to variable "j" and convert it to a character using
String.fromCharCode() (65 is ASCII value of 'A').
A B B C C C D D D D E E E E E
Pattern2 is similar to pattern1, only difference is in characters. You can see pattern2, characters are changing only in the next iteration of the external loop. So you can simply use 'i - 1' while creating characters instead of 'j' ('i - 1' because 'i' starts from 1).
A B C D E F G H I J K L M N O
This pattern is the same as pattern1 only difference is that the character is changing in each and every iteration. To achieve this you can simply create a random variable ('count' in below example) and increase it in every iteration and use it to get the character.
ABCDE ABCD ABC AB A
In this pattern just control internal loop such as it runs for 'N' times in the first iteration of the external loop, 'N - 1' times in the second iteration, and so on. To get this set initialization variable (j) less than
'n - i + 1'. Now use the initialization variable of the internal loop for character increment.
EDCBA EDCB EDC ED E
This pattern is same as pattern4 only difference is that instead of starting character to be 'A' it is the character at ASCII value 'N - 1 + 65', where 'N' is the height of the pattern.
To achieve this use
'N - 1 - j' for character creation and add 65 to it (65 is ASCII value of A).
EDCBA DCBA CBA BA A
This pattern is the same as pattern4 only difference is that in pattern4 starting character is 'A' but in this pattern ending character is 'A' in each row.
To achieve this use
'N - i - j' for character creation and add 65 to it (65 is ASCII value of A).
Pattern 7: Pyramid patten
A ABC ABCDE ABCDEFG ABCDEFGHI
This is a pyramid-shaped pattern using the alphabet, we have created the pyramid pattern using stars in the last section. Using the same technique create the pattern and instead of printing stars, print alphabets using
Pattern 8: Pyramid pattern
A BCD EFGHI JKLMNOP QRSTUVWXY
This pattern is the same as pattern7 just alphabets are increasing in each and every iteration.
To keep track of this create a variable and increment it in every iteration of the internal loop and use this variable to create alphabets.
Pattern 9: Reverse Pyramid Pattern
ABCDEFGHI ABCDEFG ABCDE ABC A
This is a reverse pyramid pattern using alphabets. Just control the formation of spaces and creation of stars in reverse order. See the code below to understand.
Compare codes of pyramid and reverse pyramid for better understanding.
Pattern 10: Diamond Pattern
A ABC ABCDE ABCDEFG ABCDEFGHI ABCDEFG ABCDE ABC A
Diamond pattern is a combination of the pyramid and reverse pyramid alphabet patterns.
Pattern 11: Hourglass Pattern
ABCDEFGHI ABCDEFG ABCDE ABC A ABC ABCDE ABCDEFG ABCDEFGHI
Hourglass pattern is a combination of the reverse pyramid and pyramid alphabet patterns.
Pattern 12: Pascal Pattern
A AB ABC ABCD ABCDE ABCD ABC AB A
Pascal pattern is same as diamon patten just remove spaces and change conditions in internal loops.