logo CodeStepByStep logo

zigzag

Language/Type: PHP recursion

Write a recursive function zigzag that accepts an integer parameter $n and that prints out $n characters as follows:

  • The middle character of the output should always be an asterisk ("*").
  • If you are asked to write out an even number of characters, then there will be two asterisks in the middle ("**").
  • Before the asterisks you should write out less-than characters ("<").
  • After the asterisks you should write out greater-than characters (">").

For example, the following calls produce the following output:

Call Output
zigzag(1); *
zigzag(2); **
zigzag(3); <*>
zigzag(4); <**>
zigzag(5); <<*>>
zigzag(6); <<**>>
zigzag(7); <<<*>>>
zigzag(8); <<<**>>>

Your function should throw an Exception if passed a value less than 1. Note that the output does not advance to the next line.

Function: Write a PHP function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.