Practical 7
Practical 7
Practical 7
Tools: Notepad Editor and Web Browser like Internet Explorer, Google Chrome or Mozilla
Firefox.
Coding:
<html>
<head>
<title>My calculator</title>
<script type="text/javascript"> function call(click_id)
{
Var v1=parseFloat(document.getElementById("ip1").value);
var v2=parseFloat(document.getElementById("ip2").value);
if(isNaN(v1) || isNaN(v2)) alert("enter a valid number");
else if(click_id=="add") document.getElementById("output").value=v1+v2;
else if(click_id=="sub") document.getElementById("output").value=v1-v2;
else if(click_id=="mul") document.getElementById("output").value=v1*v2;
else if(click_id=="div") document.getElementById("output").value=v1/v2;
}
</script>
</head>
Web Designing and Internet Applications 2
<body>
<center>
<h1> Simple Calculator Program</h1>
<table style="background-color:pink" align=="center">
<tr>
<td>
<form method="get" action="">
<div width=100% align="center">
<label>Op1<input type="text" id="ip1"/></label>
<label>Op2<input type="text" id="ip2"/></label>
<lablel>total<input type="text" id="output"/></label>
</div>
<br>
<div width=100% align="center">
<input type="button" value="+" id="add" onclick="call(this.id)"/>
<input type="button" value="-" id="sub" onclick="call(this.id)"/>
<input type="button" value="*" id="mul" onclick="call(this.id)"/>
<input type="button" value="/" id="div" onclick="call(this.id)"/>
<input type="reset" value="clear"/>
</div>
</form>
</td>
</tr>
</table>
</center>
</body>
</html>
Conclusions:
The JavaScript code is executed successfully and all the arithmetic operations (sum, product,
difference and quotient) have been performed.