Write A Script To Implement A Simple Calculator For Mathematical Operations

0

Write a script to implement a simple calculator for mathematical operations

Write A Script To Implement A Simple Calculator For Mathematical Operations

Here is a PHP script for Simple Calculator:


PROGRAM
<html>
    <head>
        <title>PHP Calculator</title>
    </head>
    <body>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <label>Enter First Number:</label>
            <input type="number" name="num1"><br><br>
            <label>Enter Second Number:</label>
            <input type="number" name="num2"><br><br>
            <label>Select Operation:</label>
            <select name="operator">
                <option value="+">Addition</option>
                <option value="-">Subtraction</option>
                <option value="*">Multiplication</option>
                <option value="/">Division</option>
            </select><br><br>
            <input type="submit" value="Calculate">
        </form>
        <?php
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $num1 = $_POST["num1"];
            $num2 = $_POST["num2"];
            $operator = $_POST["operator"];
            $result = 0;
            if ($operator == "+") {
                $result = $num1 + $num2;
            } elseif ($operator == "-") {
                $result = $num1 - $num2;
            } elseif ($operator == "*") {
                $result = $num1 * $num2;
            } elseif ($operator == "/") {
                $result = $num1 / $num2;
            }
            echo "Result: " . $result;
        }
        ?>
    </body>
</html>

OUTPUT
*If image are looking blur then click on the image for HD quality.
Write a script to implement a simple calculator for mathematical operations

Explanation
  • The HTML form is used to get input from the user, such as two numbers and the operation to perform.
  • The PHP script processes the form data by checking which operator was selected and performs the appropriate mathematical operation on the two numbers.
  • The result of the calculation is displayed on the page.

❤️ I Hope This Helps You Understand, Click Here To Do More Exercise In PHP Programming.

Post a Comment

0Comments
Post a Comment (0)