Saturday, January 21, 2012

Count Without Loops

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

Related Posts Plugin for WordPress, Blogger...