Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Write a javascript program to make a simple calculator

Answer Posted / mehta fenil & bodiwala rum

<html>
<head>
<title>Basic Calculator</title>
<script language="JavaScipt" type="text/JavaScript">
var adddigit=false;
var oldval=0;
var op="=";

function btnconcat(c)
{
if(adddigit)
{
document.getElementById("txtdisplay").value+=c;
}
else
{
document.getElementById("txtdisplay").value=c;
adddigit=true;
}
}

function operation(newop)
{
var newarg=eval(document.getElementById("txtdisplay").value)
if(op=="+")
{
oldval=oldval+newarg;
}
else if(op=="-")
{
oldval=oldval-newarg;
}
else if(op=="*")
{
oldval=oldval*newarg;
}
else if(op=="/")
{
oldval=oldval/newarg;
}
else if(op=="=")
{
oldval=newarg;
}
else
{
oldval=newarg;
}
document.getElementById("txtdisplay").value=oldval;
adddigit=false;
op=newop;
}

function backspace()
{

document.getElementById("txtdisplay").value=document.getElementById("txtdisplay").value.substring(0,document.getElementById("txtdisplay").value.length*1-1);
}

function btnce()
{
document.getElementById("txtdisplay").value="";
}

function btnc()
{
document.getElementById("txtdisplay").value=0;
oldval=0;
adddigit=false;
op="=";
}
</script>
</head>
<body>
<center>
<table border=5 cellpading=5 cellspacing=5>

<tr>
<td colspan="4"><input type="text" id="txtdisplay"
size="25" style="text-align:right"></td>
</tr>

<tr>
<td>
<center>
<input type="button" id="btnc" value="C"
onClick="btnc();">
</center>
</td>

<td>&nbsp;

</td>

<td>
<center>
<input name="Button" type="button" id="btnc"
onClick="btnce();" value="CE">
</center>
</td>

<td>
<center>
<input type="button" id="btnc" value="<="
onClick="backspace();">
</center>
</td>
</tr>

<tr>
<td>
<center>
<input type="button" id="btn7" value="7"
onClick="btnconcat(7);">
</center>
</td>

<td>
<center>
<input type="button" id="btn8" value="8"
onClick="btnconcat(8);">
</center>
</td>

<td>
<center>
<input type="button" id="btn9" value="9"
onClick="btnconcat(9);">
</center>
</td>

<td>
<center>
<input type="button" id="btnpls" value="+"
onClick="operation('+');">
</center>
</td>
</tr>

<tr>
<td>
<center>
<input type="button" id="btn4" value="4"
onClick="btnconcat(4);">
</center>
</td>

<td>
<center>
<input type="button" id="btn5" value="5"
onClick="btnconcat(5);">
</center>
</td>

<td>
<center>
<input type="button" id="btn6" value="6"
onClick="btnconcat(6);">
</center>
</td>

<td>
<center>
<input type="button" id="btnmin" value="-"
onClick="operation('-');">
</center>
</td>
</tr>

<tr>
<td>
<center>
<input type="button" id="btn1" value="1"
onClick="btnconcat(1);">
</center>
</td>

<td>
<center>
<input type="button" id="btn2" value="2"
onClick="btnconcat(2);">
</center>
</td>

<td>
<center>
<input type="button" id="btn3" value="3"
onClick="btnconcat(3);">
</center>
</td>

<td>
<center>
<input type="button" id="btnmul" value="*"
onClick="operation('*');">
</center>
</td>
</tr>

<tr>
<td>
<center>
<input type="button" id="btnpoi" value="."
onClick="btnconcat('.');">

</center>
</td>

<td>
<center>
<input type="button" id="btn0" value="0"
onClick="btnconcat(0);">
</center>
</td>

<td>
<center>
<input type="button" id="btneql" value="="
onClick="operation(value)">
</center>
</td>

<td>
<center>
<input type="button" id="btndiv" value="/"
onClick="operation('/');">
</center>
</td>
</tr>

</table>
</center>
</body>
</html>

Is This Answer Correct ?    12 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Write the code for adding new elements dynamically?

1057


What is data type in javascript?

934


What are all the types of Pop up boxes available in JavaScript?

1058


How can javascript be used to personalize or tailor a web site to fit individual users?

832


Write a Program using Servlet and JDBC for developing online application for displaying the details of Cars owned by the residents in XYZ society. Make necessary assumptions and create appropriate databases

2262


List some of the advantages of javascript.

1561


Have you used any browser for debugging? If yes, how is it done?

866


Should I learn java first or javascript?

922


How to access the value of a textbox using javascript?

980


What does undefined value mean in javascript?

1044


What javascript method would convert the string “20” to an integer (on the fly) so “20” + 20 = 40?

898


Explain the for-in loop?

879


What kind of conditional statements does JavaScript support?

924


How long does it take to learn coding?

989


Why is java better than javascript?

889