Infolinks

Breaking

Adsense

Thursday, April 21, 2016

CREATE A MULTIPLICATION TABLE USING FOR LOOP IN PHP

This is another example of a program that uses controlled structures using a looping statement. This time I did use the FOR loop using php language. On my previous article, I define loop as a structure that can execute a block of code several times as long as a specified condition is true. Loops are important to minimize codes that are executed for several times.

Can you imagine creating a multiplication table like on the output below without using iteration? I know it’s cumbersome and will take lots of your effort and time. A multiplication table program will use two (2) iterations to display rows and columns. The first loop will specify the multiplicand which is from 1 to 10 and the second loop will specify the multiplier which is from 1 to 10 also.

PHP CODE FOR MULTIPLICATION TABLE


<html>

    <body>
         <table>
         <?php
             for($multiplicand=1;$multiplicand<=10;$multiplicand++)
                {
                    echo "<tr>";
                    for($multiplier=1;$multiplier<=10;$multiplier++)
                      {
                          echo "<td>$multiplier X $multiplicand = ".$multiplicand*$multiplier."</td>";
                       }
                          echo "<tr>";
                 }
          ?>
          </table>
    </body></html>
The code above will display a multiplication from 1 to 10 like in the sample output below.  Hope this helps! Good day! ^_^

SAMPLE OUTPUT FOR MULTIPLICATION TABLE




CREATE A MULTIPLICATION TABLE USING FOR LOOP IN PHP

No comments:

Post a Comment

Adbox