PHP Exercise : Divison Table in PHP

Write a divison table program in PHP using for loop.

Solution

Here is the code to print divison table in PHP. In this code, the main body of the code has one for loop nested inside another, with each loop executing ten times and resulting in a 10 X 10 table. In the loop, each iteration of the outer loop prints a row, whereas the each inner iteration prints a cell.

<?php  
	$start  = 1;
	$end = 10;
?>
<html>
 <head>
	<title>A divison table in PHP</title>
 </head>
 <body>
	<table border="1">
	<?php
		print("<tr>");
		print("<th></th>");
		for($count = $start; $count <= $end; $count++) 
		print("<th>".$count."</th>");
		print("</tr>");
		
		for($count = $start; $count <= $end; $count++) 
		{
		  print("<tr><th>".$count."</th>");
		  for($count2 = $start; $count2 <= $end; $count2++) 
		  {
			$result = $count / $count2; 
			printf("<td>%.3f</td>", $result);
		  }
		  print("</tr> \n");
		}	
	?>
	</table>
 </body>
</html>

Output of the above code

 12345678910
1 1.000 0.500 0.333 0.250 0.200 0.167 0.143 0.125 0.111 0.100
2 2.000 1.000 0.667 0.500 0.400 0.333 0.286 0.250 0.222 0.200
3 3.000 1.500 1.000 0.750 0.600 0.500 0.429 0.375 0.333 0.300
4 4.000 2.000 1.333 1.000 0.800 0.667 0.571 0.500 0.444 0.400
5 5.000 2.500 1.667 1.250 1.000 0.833 0.714 0.625 0.556 0.500
6 6.000 3.000 2.000 1.500 1.200 1.000 0.857 0.750 0.667 0.600
7 7.000 3.500 2.333 1.750 1.400 1.167 1.000 0.875 0.778 0.700
8 8.000 4.000 2.667 2.000 1.600 1.333 1.143 1.000 0.889 0.800
9 9.000 4.500 3.000 2.250 1.800 1.500 1.286 1.125 1.000 0.900
10 10.000 5.000 3.333 2.500 2.000 1.667 1.429 1.250 1.111 1.000




Related PHP Programs for Practice

PHP - Division table program
PHP - Prime Number Program
PHP - Print numbers 10 to 1 using recursion
PHP - Loop through an associative array
PHP - Differentiate between fgets, fgetss and fgetcsv
PHP - Set session on login
PHP - Convert text to speech
PHP - Copying or moving a file
PHP - Locking a file
PHP - CURL Cookie Jar
PHP - Password Hashing
PHP - Emoji Unicode Characters




Read more articles


General Knowledge



Learn Popular Language