Javascript calculator with basic arithmetic operations

Last updated on March 1st, 2022 at 12:07 pm

Click to rate this tutorial!
[Total: 39 Average: 3.8]

Javascript calculator with basic arithmetic operations

This is a simple javascript that performs basic arithmetic operations like Addition, Subtraction, Division, Multiplication and Modulus. Just copy paste the below script in to an html file and run on any browser to see the working.

Check out Advance Calculator With Memory Function Using Javascript.

<title>Javascript calculator with basic arithmetic operations
</title><script language="javascript" type="text/javascript">
function multiply(){
a=Number(document.my_cal.first.value);
b=Number(document.my_cal.second.value);
c=a*b;
document.my_cal.total.value=c;
}
</script>

<script language="javascript" type="text/javascript">
function addition(){
a=Number(document.my_cal.first.value);
b=Number(document.my_cal.second.value);
c=a+b;
document.my_cal.total.value=c;
}
</script>

<script language="javascript" type="text/javascript">
function subtraction(){
a=Number(document.my_cal.first.value);
b=Number(document.my_cal.second.value);
c=a-b;
document.my_cal.total.value=c;
}
</script>

<script language="javascript" type="text/javascript">
function division(){
a=Number(document.my_cal.first.value);
b=Number(document.my_cal.second.value);
c=a/b;
document.my_cal.total.value=c;
}
</script>

<script language="javascript" type="text/javascript">
function modulus(){
a=Number(document.my_cal.first.value);
b=Number(document.my_cal.second.value);
c=a%b;
document.my_cal.total.value=c;
}
</script>

<!-- Opening a HTML Form. -->
<form name="my_cal">

<!-- Here user will enter 1st number. -->
Number 1: <input type="text" name="first">


<!-- Here user will enter 2nd number. -->
Number 2: <input type="text" name="second">


<!-- Here result will be displayed. -->
Get Result: <input type="text" name="total">

<input type="button" value="ADD" onclick="javascript:addition();">
<input type="button" value="SUB" onclick="javascript:subtraction();">
<input type="button" value="MUL" onclick="javascript:multiply();">
<input type="button" value="DIV" onclick="javascript:division();">
<input type="button" value="MOD" onclick="javascript:modulus();">
</form>

Demo

Click to rate this tutorial!
[Total: 39 Average: 3.8]

Leave a Reply

Your email address will not be published.