Write a Program of Simple Calculator in JavaScript using HTML.



Write a Program of Simple Calculator in JavaScript using HTML...

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

More JavaScript Interview Questions

What’s a way to append a value to an array?

0 Answers  


What are the javascript data types?

0 Answers  


What is the result of below given line of code in java script? 5+4+'7'?

0 Answers  


How to remove duplicate values from a javascript array?

0 Answers  


What is the reason for wrapping the entire content of a javascript source file in a function book?

0 Answers  






What is a boolean in javascript?

0 Answers  


What do you mean by ide?

0 Answers  


How to add buttons in javascript?

0 Answers  


Can you explain the difference between == and ===?

0 Answers  


Name the two functions that are used to create an HTML element dynamically?

0 Answers  


How host objects are different from native objects in javascript?

0 Answers  


What are the Advantages and Disadvantages of JavaScript?

0 Answers  


Categories