Write a Program of Simple Calculator in JavaScript using HTML.

Answer Posted / dhanya

<html>

<head>

<title>Simple Javascript Calculator - Basic Arithmetic
Operations</title>

<!-- Aim: Write HTML Code Using JavaScript Functions To
Perform Basic Arithmetic Operations. -->
<!-- 1. Take Two numbers from user say 'Number 1' and
'Number 2'. -->
<!-- 2. Perform Addition, Subtraction, Multiplication,
Division and Modulus. -->
<!-- 3. Result must be displayed on same HTML Page when
respective button is clicked. -->
<!-- 4. Use <input> tag (HTML Forms Concept) with onclick.-->
<!-- 5. Call individual Javascript Function, put them inside
<head> tag only.-->
<!-- 6. Javascript Tutorial/Code For Computer Science
Students. -->
<!-- 7. Tested and Written By (c) Gaurav Akrani. -->

<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>

<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>

<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>

<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>

<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 a HTML Form. -->
<form name="calculator">

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


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


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


<!-- Here respective button when clicked, calls only
respective artimetic function. -->
<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>

</body>
</html>

Is This Answer Correct ?    8 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How can a Javascript code redirect the user to a different page?

616


What is purpose of onerror event handler in javascript?

557


Is javascript multi-threaded or single-threaded?

564


Is javascript pure object oriented?

507


Do hackers use javascript?

548






What is variable typing?

627


What is the meaning of callback function?

578


java pgm for reads a file(text file) and removes all the spaces then the text and write this back into the same file. e.g: (Input) _______ chennai is fourth biggest city in india. (output) _______ chennaiisfourthbiggestcityinindia.

1635


What are .js files?

571


Are Attributes and Property the same?

652


Can I open javascript on iphone?

575


How can an HTMLCollection be traversed?

618


How can you detect the client operating system using javascript?

656


Who is the world famous hacker?

539


Name the types of functions.

584