Problem: write a program to display number from 1-10 without using any looping statements (for, while, do) and only using one parameter.
Input:
10
Output:
1 2 3 4 5 6 7 8 9 10
Solution:
<?php
$rec = new Recursion;
$rec->display(10);
class Recursion
{
public function display($num)
{
if($num > 0)
{
$this->display($num-1);
echo $num.'<br>';
}
}
}
?>
No comments:
Post a Comment