Space for google add

PHP program of arithmetic operations.

 

Write a PHP script for the following: Design a form to accept two numbers
from the user. Give options to choose the arithmetic operation (use radio buttons).
Display the result on the next form. (Use the concept of function and default
parameters. Use ‘include’ construct or require stmt)



a1setb1.html

<html>
<body>
<form method="post" action="a1setb1.php">
Enter first number :
<input type="text" name="no1" value=" "><br>
Enter Second number :
<input type="text" name="no2" value=" "><br>
<input type='radio' name='op' value='1'>Addition<br>
<input type='radio' name='op' value='2'>Multiplication<br>
<input type='radio' name='op' value='3'>Subtraction<br>
<input type='radio' name='op' value='4'>Division<br>
<input type='submit' name='submit' value='submit'>
</form>
</body>
</html>




calculator.php

<?php
function add($a,$b)
{
$s=$a+$b;
return $s;
}
function sub($a,$b)
{
$s=$a-$b;
return $s;
}
function mul($a,$b)
{
$s=$a*$b;
return $s;
}
function div($a,$b)
{
if($b==0)
echo "Division not possible.<br>";
else
$s=$a/$b;
return $s;
}
?>




a1setb1.php

<?php
include 'calculator.php';
$a=$_POST['no1'];
$b=$_POST['no2'];
$c=$_POST['op'];
switch($c)
{
case 1:
    $d=add($a,$b);
    echo "Addition :$d";break;
case 2:
$d=mul($a,$b);
    echo "Multiplication :$d";break;
case 3:
$d=sub($a,$b);
    echo "Subtraction:$d";break;
case 4:
$d=mul($a,$b);
    echo "Division:$d";break;

}
?>

Post a Comment

0 Comments