Sum Difference Multiply Divide in PHP
Q. Write a PHP program for addition, subtraction, multiplication, and division of two numbers
<?php
// Define two numbers
$num1 = 20;
$num2 = 10;
// Perform operations
$sum = $num1 + $num2;
$difference = $num1 - $num2;
$product = $num1 * $num2;
// Handle division carefully to avoid division by zero
if ($num2 != 0) {
$quotient = $num1 / $num2;
echo "Quotient: $quotient<br>";
} else {
echo "Division by zero is not allowed.<br>";
}
// Display results
echo "Sum: $sum<br>";
echo "Difference: $difference<br>";
echo "Product: $product<br>";
?>
Q. Write an algorithm for addition, subtraction, multiplication, and division in PHP
1. Start 2. Define two numbers: num1 and num2 3. Compute sum = num1 + num2 4. Compute difference = num1 - num2 5. Compute product = num1 * num2 6. If num2 ≠ 0, compute quotient = num1 / num2 7. Display all results 8. Else, display "Division by zero is not allowed" 9. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com