Q:

Write a division table program in PHP using for loop

belongs to collection: PHP Programming Exercises

0

Division Table in PHP

Write a division table program in PHP using for loop.

 

 

All Answers

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

Solution

Here is the code to print the division 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 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

  1 2 3 4 5 6 7 8 9 10
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

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

total answers (1)

PHP Programming Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a program in PHP to print prime numbers betw... >>
<< Write a php program to compare between things that...