Preview :
Code :
<html>
<html>
<head>
<title>calc</title>
</head>
<body>
<script type="text/javascript">
function add() {
var a1 = document.getElementById('n1');
var a2 = document.getElementById('n2');
document.getElementById('ans').value = parseInt(a1.value)+parseInt(a2.value);
return;
}
function sub() {
var a1 = document.getElementById('n1');
var a2 = document.getElementById('n2');
document.getElementById('ans').value = parseInt(a1.value)-parseInt(a2.value);
return;
}
function mul() {
var a1 = document.getElementById('n1');
var a2 = document.getElementById('n2');
document.getElementById('ans').value = parseInt(a1.value)*parseInt(a2.value);
return;
}
function div() {
var a1 = document.getElementById('n1');
var a2 = document.getElementById('n2');
document.getElementById('ans').value = parseInt(a1.value)/parseInt(a2.value);
return;
}
function all(id) {
if(id=='+but') add();
if(id=='-but') sub();
if(id=='*but') mul();
if(id=='/but') div();
return;
}
</script>
<table align="center">
<tr>
<td colspan="2">
<input type="text" id="n1"/>
</td>
<td colspan="2">
<input type="text" id="n2"/>
</td>
</tr>
<tr>
<td>
<input type="button" value="+" id="+but" onclick=" return add('+but')"/>
</td>
<td>
<input type="button" value="-" id="-but" onclick=" return sub('-but')"/>
</td>
<td>
<input type="button" value="*" id="*but" onclick=" return mul('*but')"/>
</td>
<td>
<input type="button" value="/" id="/but" onclick=" return div('/but')"/>
</td>
</tr>
<tr>
<td colspan="4">
<input type="text" id="ans" readonly="readonly"/>
</td>
</tr>
</table>
</body>
</html>
No comments:
Post a Comment