Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. Task:
  2. Produce a web page that has two Multiplication tables in it Each table is to be produced by one loop and you will need
  3. to demonstrate a FOR loop and a WHILE loop to complete this task, hence, creating two separate tables.
  4. Page Requirements Alternate rows shall be coloured using html attribute names Every cell containing the result of the square
  5. of a number (1x1, 2x2, 3x3 etc) shall also have distinctive background using a html attribute name Create your times table from 1 to12.
  6. (ie: 1x1 ... 12x12) - for both times tables. Please display only the result (i.e: 1, 2, 4, 144) for the FOR loop table and
  7. display the calculation and result (i.e: 1x1=1, 2x2=4, etc) for the WHILE loop table.
  8.  
  9. I have broken down the completion of this task into 3 different versions.
  10. These versions document the coding process: from table design, to operation through loops, to the final update of the table design.
  11.  
  12. <!DOCTYPE html>
  13. <html lang="en">
  14. <head>
  15. <meta charset="utf-8">
  16. <title>Times tables to 12</title>
  17. </head>
  18. <body>
  19. <h1>Multiplication tables with PHP</h1>
  20. <h2>Multiplication table using a while loop. </h2>
  21. <table width="100%" border="1" align="center">
  22. <caption fontsize="14">Multiplication tables of 1 to 12 with a "while loop".</caption>
  23. <tr style="background-color:lightblue">
  24. <th>Timetable of 1</th>
  25. <th>Timetable of 2</th>
  26. <th>Timetable of 3</th>
  27. <th>Timetable of 4</th>
  28. <th>Timetable of 5</th>
  29. <th>Timetable of 6</th>
  30. <th>Timetable of 7</th>
  31. <th>Timetable of 8</th>
  32. <th>Timetable of 9</th>
  33. <th>Timetable of 10</th>
  34. <th>Timetable of 11</th>
  35. <th>Timetable of 12</th>
  36. </tr>
  37. <?php
  38. #Declare a function with 2 arguments
  39. function times ($x, $y) {
  40. #Inside the function, multiply the 2 numbers
  41. $tables = $x * $y;
  42. #and return the result
  43. return $tables;
  44. }
  45.  
  46. #In the main PHP script, create a variable for the first number
  47. $x = 1;
  48.  
  49. #While the first number is less than 13, repeat the following loop
  50. while ($x < 13) {
  51. #Create another variable for the second number and set table row
  52. $y = 1;
  53. echo "<tr>";
  54. #While the second number is less than 13, repeat the following function
  55. while ($y < 13) {
  56. $tables = times ($x, $y);
  57. #Capture the value returned from the function and print the result
  58. echo "<td> $x X $y is: $tables </td>";
  59. #Increment the value of the second variable
  60. $y++;
  61. }
  62. #Increment the value of the first variable and close the table row
  63. $x++;
  64. echo "</tr>";
  65. }
  66.  
  67. ?>
  68. </table>
  69. <h2>Multiplication table using a for loop. </h2>
  70. <table width="100%" border="1" align="center">
  71. <caption>Multiplication tables of 1 to 12 with a "for loop".</caption>
  72. <tr style="background-color:lightblue">
  73. <th>Timetable of 1</th>
  74. <th>Timetable of 2</th>
  75. <th>Timetable of 3</th>
  76. <th>Timetable of 4</th>
  77. <th>Timetable of 5</th>
  78. <th>Timetable of 6</th>
  79. <th>Timetable of 7</th>
  80. <th>Timetable of 8</th>
  81. <th>Timetable of 9</th>
  82. <th>Timetable of 10</th>
  83. <th>Timetable of 11</th>
  84. <th>Timetable of 12</th>
  85. </tr>
  86. <?php
  87. $result = "";
  88. #For each value of i below 13, repeat the following loop
  89. for ($i = 1; $i < 13 ; $i++) {
  90. #Open a table row
  91. echo "<tr>";
  92. #For each value of n up to 13, repeat the operation and print the results
  93. for ($n = 1; $n < 13 ; $n++) {
  94. $result = $i * $n;
  95. #Test results for sauare numbers and print special font
  96. if ($result ? $i%$n == 0 && $i==$n: $i%$n !== 0) {
  97. echo "<td> <b>$result</b></td>";
  98. }else {
  99. echo "<td> $result </td>";}
  100. }
  101. #Close the table row
  102. echo "</tr>";
  103.  
  104. }
  105.  
  106. ?>
  107. </table>
  108. </body>
  109. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement