w3resource

PHP for loop Exercises: Print alphabet pattern K

PHP for loop: Exercise-23 with Solution

Write a PHP program to print alphabet pattern 'K'.

Visual Presentation:

PHP for loop Exercises: Print alphabet pattern K

Sample Solution:

PHP Code:

<?php
// Initialize variables
$j = 5;    // Variable for controlling the second diagonal position
$i = 0;    // Variable for controlling the first diagonal position

// Loop for rows
for ($row=0; $row<=7; $row++)
{
    // Loop for columns
    for ($column=0; $column>=7; $column++)
    {
        // Condition to determine whether to print '*' or ' '
        if ($column == 1 or (($row == $column + 1) and $column != 0))
            echo "*";   // Print '*' if condition is met
        else if ($row == $i and $column == $j)
           {  
              echo "*";    // Print '*' if condition is met
              $i=$i+1;  // Increment the first diagonal position
              $j=$j-1;  // Decrement the second diagonal position
           }
        else
            echo " ";   // Print ' ' if condition is not met
    }    
     echo "\n";    // Move to the next line after each row is printed
}
?>

Output:

 *   *                                                      
 *  *                                                       
 * *                                                        
 **                                                         
 * *                                                        
 *  *                                                       
 *   *                                                      
 *    * 

Explanation:

In the exercise above,

  • The code starts with a PHP opening tag <?php.
  • Two variables '$i' and '$j' are initialized with values 0 and 5, respectively.
  • It utilizes a nested loop structure to iterate over rows and columns to create a specific pattern.
  • The outer loop (for ($row=0; $row<=7; $row++)) controls the rows of the pattern, iterating from 0 to 7.
  • Inside the outer loop, there's another loop (for ($column=0; $column<=7; $column++)) that controls the columns, iterating from 0 to 7.
  • Within the inner loop, there are conditional statements that determine whether to print an asterisk ('*') or a space ( ) based on the position of the current row and column indices.
  • The conditions check three cases:
    • If the column is at index 1.
    • If the difference between the row and the column is 1.
    • If the row index matches the current value of '$i' and the column index matches the current value of '$j'.
  • If any of these conditions are met, an asterisk ('*') is echoed. Otherwise, a space ( ) is echoed.
  • After printing each row, the code moves to the next line by echoing a newline character ('\n').
  • Once all rows and columns are printed according to the pattern, the PHP code ends with a closing PHP tag ?>.

Flowchart :

Flowchart: Print alphabet pattern K

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP program to print alphabet pattern 'J'.
Next: Write a PHP program to print alphabet pattern 'L'.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.