
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP Program to Print Multiplication Table
A multiplication table is a useful mathematical tool that is used to print the products of a given number with a range of values, generally, it is printed from 1 to 10. In this article, we are going to learn multiple ways to generate and print multiplication tables in PHP.
Formula to Generate Multiplication Table
To create a multiplication table for any number, use the formula:
Answer = Number à MultiplierWhere:
number is the fixed value for which the table is generated.
Multiplier is the range of values (e.g., 1 to 10).
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
Using a Direct Loop
This is a simple and direct method to print multiplication tables. In this method, we use a simple loop to calculate and display the multiplication table for a given number. It uses a basic iteration working, where the program calculates each multiple of the given number step by step. This approach is suitable for beginners and works efficiently for small ranges such as for generating tables up to 10 or 20.
Steps for Implementation
- Define the number.
- Now, use a loop to iterate through the range (e.g., 1 to 10).
- Multiply the number by the current loop value and print the result.
- Print the Output.
Implementation Code
<?php // Define the number $number = 5; // Print the multiplication table for ($i = 1; $i <= 10; $i++) { $result = $number * $i; echo "$number x $i = $result
"; // Using newline for output formatting } ?>
Output
5 x 1 = 5Time Complexity: O(1)
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
Space Complexity: O(1)
Using a Function
In this approach, we use a function to generate a multiplication table. This approach uses a reusable function to generate multiplication tables, which makes it easy to work with different inputs. This is generally useful in larger programs or when the table needs to be calculated for different numbers.
Steps for Implementation
- Define a function that accepts the number as a parameter.
- Now, use a loop inside the function to calculate and print the table.
- Call the function with numbers from 1 to 10.
Implementation Code
<?php // Function to generate a multiplication table function printMultiplicationTable($number) { for ($i = 1; $i <= 10; $i++) { $result = $number * $i; echo "$number x $i = $result
"; // Using newline for output formatting } } // Call the function with a specific number $number = 7; printMultiplicationTable($number); ?>
Output
7 x 1 = 7Time Complexity: O(1)
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
Space Complexity: O(1)
Real-World Applications
- Multiplication tables are used to teach basic arithmetic calculations.
- It is used to Automate repetitive arithmetic operations in various scripts.
- It is used to generate reports and charts for web applications.