Home > Backend Development > PHP Tutorial > PHP Program to Print Multiplication Table

PHP Program to Print Multiplication Table

Mary-Kate Olsen
Release: 2025-02-07 11:10:09
Original
282 people have browsed it

PHP Program to Print Multiplication Table

Multiplication tables are fundamental mathematical tools used to display the products of a number multiplied by a range of values, typically from 1 to 10. This article explores several PHP methods for generating and displaying these tables.

The core formula for generating a multiplication table is straightforward:

<code>Result = Number × Multiplier</code>
Copy after login

Where:

  • Number: The base number for which the table is generated.
  • Multiplier: The range of values (e.g., 1 to 10).

Let's illustrate with examples:

Example 1:

  • Input: Number = 5
  • Output: 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

Example 2:

  • Input: Number = 3
  • Output: 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30

Example 3:

  • Input: Number = 0
  • Output: 0 x 1 = 0 0 x 2 = 0 0 x 3 = 0 0 x 4 = 0 0 x 5 = 0 0 x 6 = 0 0 x 7 = 0 0 x 8 = 0 0 x 9 = 0 0 x 10 = 0

Below, we'll examine different PHP approaches for generating multiplication tables.

Method 1: Direct Loop

This simple method uses a for loop to iterate and calculate each multiple. It's efficient for smaller ranges.

Implementation:

<?php
$number = 5;

for ($i = 1; $i <= 10; $i++) {
    $result = $number * $i;
    echo "$number x $i = $result\n";
}
?>
Copy after login

Output: (Note the n provides a newline character for better formatting)

<code>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</code>
Copy after login

Time Complexity: O(n) Space Complexity: O(1)

Method 2: Using a Function

This approach employs a function for reusability and better code organization.

Implementation:

<?php
function generateTable($number) {
    for ($i = 1; $i <= 10; $i++) {
        echo "$number x $i = " . ($number * $i) . "\n";
    }
}

$number = 7;
generateTable($number);
?>
Copy after login

Output:

<code>7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70</code>
Copy after login

Time Complexity: O(n) Space Complexity: O(1)

Practical Applications:

Multiplication tables are essential in:

  • Educational tools: Teaching basic arithmetic.
  • Software automation: Automating repetitive calculations within scripts.
  • Data generation: Creating data for reports and charts.

This article provided two clear and efficient ways to generate multiplication tables in PHP, catering to different programming styles and needs. The choice between a direct loop and a function depends on the context of your application.

The above is the detailed content of PHP Program to Print Multiplication Table. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template