simple calculator in javascript

simple calculator

simple calculator in javascript

simple calculator in javascript:-Hi Readers,to make simple calculator in javascript is very easy and intresting.In this simple calculator we have functionalty of add,minus,multiplication,divide and square root of number

 

Source Of simple calculator

To make this, We have used some predefined function of javascript like

var x=document.getElementById("val1").value;
//getElementById get the value of element of textbox with id=val1.
var sum=parseInt(x) + parseInt(y); //addition of two values
//parseInt is used to convert the string into integer type.
document.getElementById("result").value=sqt.toString();
//converting integer value into string using toString()method

var sqt=(Math.sqrt(parseInt(x)));

 

Source Code of simple calculator in javascript

 

<!DOCTYPE html>
<html>
<head>
<title>simple calculator in javascript</title>
<head>
<script type="text/javascript">
function sum()
{
var x=document.getElementById("val1").value;
//getElementById get the value of element with id=val1.

var y=document.getElementById("val2").value;
//getElementById get the value of element with id=val2.

var sum=parseInt(x) + parseInt(y); //addition of two values
//parseInt is used to convert the string into integer type.
document.getElementById("result").value=sum.toString();

//converting integer value into string using toString()method

}
function minus()
{
var x=document.getElementById("val1").value;
//getElementById get the value of element with id=sum1.

var y=document.getElementById("val2").value;
//getElementById get the value of element with id=sum2.

var sub=parseInt(x) - parseInt(y); //subtraction of two values
//parseInt is used to convert the string into integer type.
document.getElementById("result").value=sub.toString();
//converting integer value into string using toString()method
}
function multiply()
{
var x=document.getElementById("val1").value;
var y=document.getElementById("val2").value;
var mul=parseInt(x)*parseInt(y);
//parseInt is used to convert the string into integer type.

document.getElementById("result").value=mul.toString();
//converting integer value into string using toString()method
}
function divide()
{
var x=document.getElementById("val1").value;
var y=document.getElementById("val2").value;
var divide=parseInt(x)/parseInt(y);
document.getElementById("result").value=divide.toString();
//converting integer value into string using toString()method
}
//percentage function to get %
function per()
{
var x=document.getElementById("val1").value;
var y=document.getElementById("val2").value;
var per=(parseInt(x)/parseInt(y))*100;
document.getElementById("result").value=per.toString();
//converting integer value into string using toString()method
}
//square root function to get the root of a value.
function squareRoot()
{
var x=document.getElementById("val1").value;
var sqt=(Math.sqrt(parseInt(x)));
document.getElementById("result").value=sqt.toString();
//converting integer value into string using toString()method
}
</script>
</head>
<body>
Input1:
<input type="text" name="text1" id="val1"/>
Input2:
<input type="text" name="text2" id="val2" />
<div style="margin-top:20px;">
Result(=):
<input type="text" name="result" id="result" />
</div>

<div>
<p>
<input type="button" onclick="sum()" value="SUM(+)" />
<input type="button" onclick="minus()" value="Minus(-)" />
<input type="button" onclick="multiply()" value="Multiply(*)" />
<input type="button" onclick="divide()" value="Divide(/)" />
<input type="button" onclick="per()" value="percentage(%)" />
<input type="button" onclick="squareRoot()" value="Square Root" />
</p>
</div>
</body>
</html>

I Hope You would like this Topic

 

 

 

Leave a Comment