473,322 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Code for calculator

10
I am looking for HTML code that accepts two numbers and do some calculations ie add, subtract, multipy and divide. how do i go about it
May 22 '07 #1
3 8808
drhowarddrfine
7,435 Expert 4TB
HTML doesn't do this. You need a programming language. On the client side, it's javascript.
May 22 '07 #2
AricC
1,892 Expert 1GB
I am looking for HTML code that accepts two numbers and do some calculations ie add, subtract, multipy and divide. how do i go about it
Like Doc said you need Javascript here is an example:
[html]
<script language="javascript">

// --------------------------------------------------------------------------------
// Name: btnCalculateTotal_Click
// Abstract: Add 2 Values and update total
// --------------------------------------------------------------------------------
function btnCalculateTotal_Click()
{

var sngValue1 = 0;
var sngValue2 = 0;
var strOperation = "";
var sngTotal = 0;

// Get values from form
sngValue1 = Number(frmSimpleCalculator.txtValue1.value);
sngValue2 = Number(frmSimpleCalculator.txtValue2.value);
strOperation = frmSimpleCalculator.cmbOperation.value;

// Determine what operation to do
switch ( strOperation )
{
case "1": sngTotal = sngValue1 + sngValue2;
break;


}

// Print Value back out to screen
frmSimpleCalculator.txtTotal.value = sngTotal;
}


// --------------------------------------------------------------------------------
// Name: btnClear_Click
// Abstract: clear the screen
// --------------------------------------------------------------------------------
function btnClear_Click()
{
// Set Textboxes to default
frmSimpleCalculator.txtValue1.value = 0;
frmSimpleCalculator.txtValue2.value = 0;
frmSimpleCalculator.txtTotal.value = 0;

// Reset Focus
frmSimpleCalculator.txtValue1.focus( );

}



</script>
<html>
<head>
<title>Calculator</title>
</head>
<body bgcolor="#aaaaaa">

<table style="background-color:black; color:white; width:800">
<tr>
<td>
<strong>Homework 1</strong>
</td>
</tr>
</table>

<form name="frmSimpleCalculator" action="" method="get">
<fieldset name="fraSimpleCalculator" style="width:1">
<legend>Simple Calculator</legend>
<table width="180" border="0">

<!-- Value 1 -->
<tr>
<td align="left">
Value 1:
</td>
<td align="right">
<input type="text" value="0" name="txtValue1" size="10" maxlength="5" style="text-align:right">
</td>
</tr>

<!-- Operation -->
<tr>
<td align="center" colspan="2">
<select name="cmbOperation" size="1">
<option value="1" selected>+
<option value="2">-
<option value="3">*
<option value="4">/
</select>
</td>
</tr>

<!-- Value 2 -->
<tr>
<td align="left">
Value 2:
</td>
<td align="right">
<input type="text" value="0" name="txtValue2" size="10" maxlength="5" style="text-align:right">
</td>
</tr>

<!-- Horizontal rule -->
<tr>
<td align="center" colspan="2">
<hr />
</td>
</tr>

<!-- Total -->
<tr>
<td align="left">
Total:
</td>
<td align="right">
<input type="text" value="0" name="txtTotal" size="10" maxlength="5" style="text-align:right">
</td>
</tr>

<!-- Calculate Button -->
<tr>
<td align="center" colspan="2">
<input type="button" value="Calculate Total" name="btnCalculateTotal" style="width:150" OnClick="btnCalculateTotal_Click();">
</td>
</tr>

<!-- Clear Button -->
<tr>
<td align="center" colspan="2">
<input type="button" value="Clear" name="btnClear" style="width:150" OnClick="btnClear_Click();">
</td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
[/html]

I removed part of the code so you can try for yourself.
May 23 '07 #3
AricC
1,892 Expert 1GB
Edit: Doc is that how the new code tags are supposed to show up?
May 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
4
by: 0to60 | last post by:
I'm trying to create a .dll with VS.NET 2003 Architect that contains a math computational component. I need the guts of the thing to be in native code, as performance is key for that part. But, I...
3
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
1
by: gzeng | last post by:
Hi Everybdy: I am a beginner in C#. I'd like to write a simple online calculator in C#. This calculator will ask a user to input two numbers and then add them and display the result. So, it has...
3
by: karpakavallli | last post by:
i need the code and the explanation to the code calculator
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
0
Curtis Rutland
by: Curtis Rutland | last post by:
Have any of you ever used a Reverse Polish Notation calculator? I did in high school. It was easily the best calculator ever (the HP=32SII). RPN is great, because you don't have to use parenthesis....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.