PHP Program to print the multiplication table of given number


October 23, 2023, Learn eTutorial
3701

Here in this PHP program, we are going to print the multiplication table that the user wants to see.

Related Program: Print the multiplication table of a given number using PHP forms

Multiplication Table

In this program, we accept a number from the user and display the multiplication table of that number up to 10.

For example, if the inputted number is 5 it will print the multiplication table of 5 up to 10.

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

How to print the multiplication table using PHP

First, we have to read the input from the user and store it in a variable num. Start a for loop from i =1 to 10 for printing the multiplication table of user input number. The loop iterates 10 times and print the computed result of num x i in the format of the multiplication table.

We can also generate this program using PHP forms please refer to the program to Print the multiplication table of the given number using PHP forms


We require knowledge in the following topics to do this program, Let's have a look at them

ALGORITHM

Step 1: Accept the input from the user and store it to the variable num

Step 2: Using a for loop with condition i <= 10 do the following step (i) incrementing the value of variable i in every iteration

        (i) Print the result of num x i using echo as the multiplication of the entered number

PHP Source Code

                                          <?php

  $num = readline("Enter the number: ");
  echo "Multiplication table of ",$num," is\n\n";     
  for ($i = 1; $i <= 10; $i++) {
    echo "\t", $num, " x ", $i, " = ", $num * $i, "\n";
  }
  
?>
                                      

OUTPUT

Enter the number: 8
Multiplication table of 8 is

        8 x 1 = 8
        8 x 2 = 16
        8 x 3 = 24
        8 x 4 = 32
        8 x 5 = 40
        8 x 6 = 48
        8 x 7 = 56
        8 x 8 = 64
        8 x 9 = 72
        8 x 10 = 80