Practical - 2 IWD

 

Aim

To write a PHP script to implement a simple calculator that performs basic mathematical operations.


Algorithm

  1. Declare two numbers.

  2. Select the required mathematical operation.

  3. Perform the calculation using operators.

  4. Display the result.


Program Code (PHP Script)

<?php // Declare variables $a = 20; $b = 10; // Addition $add = $a + $b; // Subtraction $sub = $a - $b; // Multiplication $mul = $a * $b; // Division $div = $a / $b; // Display results echo "Addition: " . $add . "<br>"; echo "Subtraction: " . $sub . "<br>"; echo "Multiplication: " . $mul . "<br>"; echo "Division: " . $div; ?>

Output

Addition: 30 Subtraction: 10 Multiplication: 200 Division: 2

Result

The PHP script successfully performed addition, subtraction, multiplication, and division using a simple calculator logic.


Viva-Related Questions

  1. Which operators are used for calculations in PHP?
    +, -, *, /

  2. What happens if division by zero occurs?
    → It generates a warning and returns infinity or error.

  3. Can user input be taken in PHP?
    → Yes, using HTML forms and PHP $_POST or $_GET.

Comments

Popular posts from this blog

Unit I - Introduction to PHP

Practical - 1 IWD

Practical - 10 IWD