Q:

PHP program to print the table of a given number using recursion

0

Here, we will print the table of a specified number using the recursive call of a user-defined function. Function calling itself is known as a recursive function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to print the table of a given number using recursion is given below. The given program is compiled and executed successfully.

<?php
//php program to print table of a given number 
//using recursion.
function PrintTable($num, $temp)
{
    if ($temp <= 10)
    {
        echo "$num X $temp = " . $num * $temp . "<br>";
        PrintTable($num, $temp + 1);
    }
}

PrintTable(5, 1);
?>

Output:

5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50

Explanation:

In the above program, we created a recursive function PrintTable(), here we made 10 recursive calls to print the table of a specified number with the help of if condition, the function gets finished when the value of $temp is greater than 10.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now