PHP for loop Exercises: Print alphabet pattern C
15. Alphabet Pattern 'C'
Write a PHP program to print alphabet pattern 'C'.
Visual Presentation:

Sample Solution:
PHP Code:
Output:
*** * * * * * * * ***
Explanation:
In the exercise above,
- The code starts with a PHP opening tag <?php.
- It uses 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 6.
- 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's a conditional statement that determines whether to print an asterisk ('*') or a space ( ) based on the position of the current row and column indices.
- The condition checks multiple conditions:
- If the column is at index 1 and the row is not at indices 0 or 6.
- If the row is at index 0 or 6 and the column is between indices 2 and 4 (inclusive).
- If the column is at index 5 and the row is at index 1 or 5.
- If any of these conditions are met, an asterisk ('*') is echoed out. Otherwise, a space ( ) is echoed out.
- 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 :

For more Practice: Solve these Related Problems:
- Write a PHP script to print the letter 'C' using loops, ensuring that the open side is properly aligned.
- Write a PHP function to generate an alphabet pattern for 'C' of variable size, using nested loops for control.
- Write a PHP program to construct the pattern for 'C' and incorporate conditional spacing for top and bottom rows.
- Write a PHP script to display an 'C' pattern with both inner and outer borders defined by stars and spaces.
Go to:
PREV : Alphabet Pattern 'B'.
NEXT : Alphabet Pattern 'D'.
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.