Print Table Using Do While , While & For Loop.

<?php
/*
Table Print 
*/
$num = 1;
$end = 10;
$table = 5;
/*
Do While 
*/
echo " <h3>Do While</h3> ";
do{
	$res = $table * $num;
	echo "$table * $num = $res </br>";
	$num++;
}
While($num<=$end);
//While Loop
$num = 1;
echo "<h3> While Loop </h3>";
while ($num <= $end) {
	$res = $table * $num;
	echo "$table * $num = $res </br>";
	$num++;
}
//For Loop 
echo " <h3>For Loop </h3>";
for($num = 1; $num<=10;$num++){
	$res = $table * $num;
	echo "$table * $num = $res </br>";
}

// For Loop With Table 
echo "<table>";
echo "<thead><th>Table</th><th>Multiple</th><th>Number</th><th>Equal</th><th>Result</th></thead>";
echo "<tbody>";
for($num = 1; $num<=10;$num++){ 
$res = $table * $num; 
echo "<tr><td>$table</td><td> x </td><td>$num</td><td> = </td><td>$res</td></tr>";
//Row Printing
}
echo "</tbody></table>";
?>

 

OUTPUT

Do While

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

While Loop

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

For Loop

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

For Loop With Table

TableMultipleNumberEqualResult
15x1=15
15x2=30
15x3=45
15x4=60
15x5=75
15x6=90
15x7=105
15x8=120
15x9=135
15x10=150

DEMO

(Visited 289 times, 1 visits today)
Share with Friends :
Written by:

Leave a Reply

Your email address will not be published. Required fields are marked *