Java script using Basic Arithmetic Operations


This  basic arithmetic operations are addition, subtraction, multiplication,division and mod.

Addition (+)
combines two numbers, the addends or terms, into a single number, the sum of the numbers.ex(a+b).


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

Subtraction (−)
 Subtraction is the opposite of addition. Subtraction finds the difference between two numbers, the minuend minus the subtrahend. ex(a-b)

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

Multiplication (*)
          Multiplication is the second basic operation of arithmetic. Multiplication also combines two numbers into a single number, the product.
The two original numbers are called the multiplier and the multiplicand, sometimes both simply called factors.
ex(a*b)

<script language="javascript" type="text/javascript">
function multiply(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a*b;
document.calculator.total.value=c;
}
</script>

Division (/)

Division is essentially the opposite of multiplication. Division finds the quotient of two numbers, the dividend divided by the divisor.ex(a/b)

<script language="javascript" type="text/javascript">
function multiply(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a*b;
document.calculator.total.value=c;
}
</script>

Demo :




Simple Arithmetic Operation
   Number 1   
   Number 2:   
Get Result:   

Source Code


<html>
<head>
<title>Basic Arithmetic Operations + - * / MOD Using Javascript</title>
<!--Addtion-->
<script language="javascript" type="text/javascript">
function addition(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a+b;
document.calculator.total.value=c;
}
</script>

<!--Subtraction-->
<script language="javascript" type="text/javascript">
function subtraction(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a-b;
document.calculator.total.value=c;
}
</script>

<!--Multiply-->
<script language="javascript" type="text/javascript">
function multiply(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a*b;
document.calculator.total.value=c;
}
</script>

<!--Division-->
<script language="javascript" type="text/javascript">
function division(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a/b;
document.calculator.total.value=c;
}
</script>

<!--modulus-->
<script language="javascript" type="text/javascript">
function modulus(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a%b;
document.calculator.total.value=c;
}
</script>
</head>
<body>
<!-- Opening HTML Form. -->
<form name="calculator">
<table width="21%" height="111" border="1" align="center" cellpadding="0" cellspacing="0">
<tr><th colspan="2" scope="col" bgcolor="#999999" height="40"> Simple Arithmetic Operation</th></tr>
<tr>
<td width="43%" height="40">&nbsp;&nbsp;&nbsp;Number 1</td>
<td width="57%">&nbsp;&nbsp;<input type="text" name="number1"></td>
</tr>
<tr>
<td height="40">&nbsp;&nbsp;&nbsp;Number 2:</td>
<td>&nbsp;&nbsp;<input type="text" name="number2"> </td>
</tr>
<tr>
<td colspan="2" align="center" height="40">
Get Result:&nbsp;&nbsp;&nbsp;<input type="text" name="total">
</td>
</tr>
<tr>
<td colspan="2" align="center" height="40">
<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();">
</td>
</tr>
</table>
</form>
<!-- Close HTML Form. -->
</body>
</html>

No comments:

Post a Comment