Program Code- Simple Calculator

This is a simple calculator program to perform

  • addition
  • subtraction
  • multiplication
  • division

between two numbers.

This code runs as javascript and this code can also be used as VBScript. For that, you need to change line 6 to <script language=”VBScript”> and the HTML file should be opened on old Internet Explorer.

<html>
<head>
<title>simple calculator</title>
</head>
<body>
<script language ="javascript">
function add()
{
var a = parseInt(form1.text1.value);
var b = parseInt(form1.text2.value);
form1.text3.value=a+b;
/* This function used for addition */
}
function sub()
{
var a = parseInt(form1.text1.value);
var b = parseInt(form1.text2.value);
form1.text3.value=a-b;
/* This function used for subtraction */
}
function mul()
{
var a = parseInt(form1.text1.value);
var b = parseInt(form1.text2.value);
form1.text3.value=a*b;
/* This function is used for multiplication */
}
function div()
{
var a = parseInt(form1.text1.value);
var b = parseInt(form1.text2.value);
form1.text3.value=a/b;
/* This function is used for division*/
}
</script>
<form name = form1>
<H2 align =center> SIMPLE CALCULATOR</H2>
<table align ="center">
<tr>
<td>
Enter value for A 
</td>
<td>
<input type = text name = text1>
</td>
</tr>
<tr>
<td>
Enter value for B
</td>
<td><input type = text name = text2>
</td>
</tr>
<tr>
<td>
<input type = button value ="Addition" onclick=add()>
<input type = button value ="Subtraction" onclick=sub()></td>
<td>
<input type = button value ="Multiplication" onclick=mul()>
<input type = button value ="Division" onclick=div()>
    <!--The buttons are used called the functions onclick -->
</td> 
</tr>
<tr>
<td>
Result
</td>
<td>
<input type = text name = text3>
</td>
</tr>
</table>
</form>
</body>
</html>