PHP program to print continuous character pattern triangle

To print continuous character pattern triangle in PHP, we use nested loops to generate a triangular pattern where each row contains consecutive alphabets. The pattern starts with 'A' and continues sequentially across all rows.

Example

Here's how to create a continuous character pattern triangle ?

<?php
function continuous_alphabets($val)
{
    $num = 65; // ASCII value of 'A'
    for ($m = 0; $m < $val; $m++)
    {
        for ($n = 0; $n <= $m; $n++)
        {
            $ch = chr($num);
            echo $ch . " ";
            $num = $num + 1;
        }
        echo "
"; } } $val = 6; continuous_alphabets($val); ?>

Output

A 
B C 
D E F 
G H I J 
K L M N O 
P Q R S T U 

How It Works

The function uses two nested loops to create the triangular pattern. The outer loop controls the number of rows, while the inner loop prints the characters for each row. Starting with ASCII value 65 (character 'A'), the chr() function converts each number to its corresponding alphabet character. After printing each character, the ASCII value increments to get the next consecutive letter.

Conclusion

This pattern demonstrates how to combine nested loops with ASCII character conversion to create continuous alphabetical triangles. The key is incrementing the ASCII value continuously across all rows rather than resetting it for each row.

Updated on: 2026-03-15T09:03:05+05:30

525 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements