593
Alphabet Triangle Method
There are three methods to print the alphabets in a triangle or in a pyramid form.
- range() with for loop
- chr() with for loop
- range() with foreach loop
Logic:
- Two for loops are used.
- First for loop set conditions to print 1 to 5 rows.
- Second for loop set conditions in decreasing order.
Using range() function
This range function stores values in an array from A to Z. here, we use two for loops.
Example:
Output:
Using chr() function
Here the chr() function returns the value of the ASCII code. The ASCII value of A, B, C, D, E is 65, 66, 67, 68, 69 respectively. Here, also we use two for loops.
Example:
Output:
Using range() function with foreach
In this methods we use foreach loop with range() function. The range() function contain values in an array and returns it with $char variable. The for loop is used to print the output.
Example:
Output:
Next TopicAlphabet Triangle Pattern