0% found this document useful (0 votes)
12 views6 pages

Multiplication Table

Uploaded by

23pca005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Multiplication Table

Uploaded by

23pca005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Write a PHP program to display multiplication table

AIM:

To Write a php program to display multiplication table

Algorithm:

Step 1: Start the Program:

 Initialize the HTML document.

Step 2: Set Up the HTML Head Section:

Step 3: Create the Body Section:

 Add a header <h1> with the title "Display Multiplication Tables".

Step 4 : Create the Input Form:

 Add a <form> element with method set to "POST":


o Include a <label> for "Enter a number".
o Add an <input> field of type "number" for the user to enter the number.
Ensure it is required.
o Include a <button> of type "submit" labeled "Show Table".

Step 5: Process Form Submission:

 Use PHP to check if the request method is POST:


o If $_SERVER["REQUEST_METHOD"] is equal to "POST", continue to the next
steps.

Step 6: Get the input value from the form using $_POST['number'] and assign it to a
variable called $number.

Step 7: Display the Multiplication Table:

 Output an <h2> header showing "Multiplication Table for [number]".


 Start an HTML <table> element.
 Create a header row with two columns:
o "Multiplier"
o "Result"
Step 8: Generate the Table Rows:

 Use a for loop to iterate from 1 to 10:


o Inside the loop:
1. Calculate the result by multiplying $number with the current loop
index $i.
2. Create a new row (<tr>) in the table:
 First cell (<td>): Display the multiplication expression
($number x $i).
 Second cell (<td>): Display the calculated result.

Step 9: Close the Table:

 End the table element after exiting the loop.

Step 10: End the Program:

 Close the HTML body and document.

Program:

!DOCTYPE html>
<html lang="en">
<head>
<me<ta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Multiplication Tables</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 50px;
}
table {
border-collapse: collapse;
width: 50%;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: center;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>

<h1>Display Multiplication Tables</h1>

<form method="POST" action="">


<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required><br><br>
<button type="submit">Show Table</button><br>
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];

echo "<h2>Multiplication Table for $number</h2>";


echo "<table>";
echo "<tr><th>Multiplier</th><th>Result</th></tr>";

for ($i = 1; $i <= 10; $i++)


{
$result = $number * $i;
echo "<tr><td>$number x $i</td><td>$result</td></tr>";
}

echo "</table>";
}
?>
</body>
</html>
Output:
Result:
Hence the above program is executed successfully and the output is verified

You might also like