0% found this document useful (0 votes)
0 views

lampPartA4

Uploaded by

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

lampPartA4

Uploaded by

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

Question 4

Write a Php program to generate a simple calculator.

<html>
<head>
<title>Program Four</title>
</head>
<?php
if (isset($_POST["operator"])) {
$first_num = $_POST["first_num"];
$second_num = $_POST["second_num"];
$operator = $_POST["operator"];
$result = "";

if (is_numeric($first_num) && is_numeric($second_num)) {


switch ($operator) {
case "+": $result = $first_num + $second_num;
break;

case "-": $result = $first_num - $second_num;


break;

case "*": $result = $first_num * $second_num;


break;

case "/": $result = $first_num / $second_num;


break;
}
}
}
?>
<body>
<h1>PHP Simple Calculator</h1>

<form action="" method="post">


<p>
<b>First Number:</b>
<input type="number" name="first_num" value="<?php echo $first_num; ?>" required>
</p>

<p>
<b>Second Number:</b>
<input type="number" name="second_num" value="<?php echo $second_num; ?>" required>
</p>

Question 4 1
<p>
<b>Result:</b>
<input type="number" name="result" value="<?php echo $result; ?>" readonly>
</p>

<input type="submit" value="+" name="operator">


<input type="submit" value="-" name="operator">
<input type="submit" value="*" name="operator">
<input type="submit" value="/" name="operator">
</form>
</body>
</html>

Output:

Question 4 2

You might also like