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

Here, we are going to learn how to print the table of a given number using recursion in PHP?
Submitted by Nidhi, on November 19, 2020

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.

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.