Pages

Sunday, December 11, 2011

Simple JavaScript Calculator Code - JavaScript Arithmetic


square Write HTML Code For Simple JavaScript Calculator


Yesterday I revised the concept of HTML Forms and JavaScript. While revising I encountered a University question stating, "Q. Write A HTML Code To Make Simple Calculator Using Javascript Functions and HTML Forms. Calculator Must Take Any Two Numbers from User and Perform 5 Basic Arithmetic Operations i.e Addition, Subtraction, Multiplication, Division and Modulo".

Simple JavaScript Calculator

I gave it a try to write this small calculator application (using Javascript and HTML Forms) and managed to produce following working script. This Javascript application is very simple, necessarily commented and is easy to understand. I hope it may prove useful to those interested in learning javascript functions, hence feel like sharing. To understand more, How Javascript Works? I recommend you refer this JavaScript Tutorial.

square JavaScript Code - Simple JavaScript Calculator


view plainprint?
  1. <html>  
  2.   
  3. <head>  
  4.   
  5. <title>Simple Javascript Calculator - Basic Arithmetic Operations</title>  
  6.   
  7. <!-- Aim: Write HTML Code Using JavaScript Functions To Perform Basic Arithmetic Operations. -->  
  8. <!-- 1. Take Two numbers from user say 'Number 1' and 'Number 2'. -->  
  9. <!-- 2. Perform Addition, Subtraction, Multiplication, Division and Modulus. -->  
  10. <!-- 3. Result must be displayed on same HTML Page when respective button is clicked. -->  
  11. <!-- 4. Use <input> tag (HTML Forms Concept) with onclick.-->  
  12. <!-- 5. Call individual Javascript Function, put them inside <head> tag only.-->  
  13. <!-- 6. Javascript Tutorial/Code For Computer Science Students. -->  
  14. <!-- 7. Tested and Written By (c) Gaurav Akrani. -->  
  15.   
  16. <script language="javascript" type="text/javascript">  
  17. function multiply(){  
  18. a=Number(document.calculator.number1.value);  
  19. b=Number(document.calculator.number2.value);  
  20. c=a*b;  
  21. document.calculator.total.value=c;  
  22. }  
  23. </script>  
  24.   
  25. <script language="javascript" type="text/javascript">  
  26. function addition(){  
  27. a=Number(document.calculator.number1.value);  
  28. b=Number(document.calculator.number2.value);  
  29. c=a+b;  
  30. document.calculator.total.value=c;  
  31. }  
  32. </script>  
  33.   
  34. <script language="javascript" type="text/javascript">  
  35. function subtraction(){  
  36. a=Number(document.calculator.number1.value);  
  37. b=Number(document.calculator.number2.value);  
  38. c=a-b;  
  39. document.calculator.total.value=c;  
  40. }  
  41. </script>  
  42.   
  43. <script language="javascript" type="text/javascript">  
  44. function division(){  
  45. a=Number(document.calculator.number1.value);  
  46. b=Number(document.calculator.number2.value);  
  47. c=a/b;  
  48. document.calculator.total.value=c;  
  49. }  
  50. </script>  
  51.   
  52. <script language="javascript" type="text/javascript">  
  53. function modulus(){  
  54. a=Number(document.calculator.number1.value);  
  55. b=Number(document.calculator.number2.value);  
  56. c=a%b;  
  57. document.calculator.total.value=c;  
  58. }  
  59. </script>  
  60.   
  61. </head>  
  62.   
  63. <body>  
  64.   
  65. <!-- Opening a HTML Form. -->   
  66. <form name="calculator">  
  67.   
  68. <!-- Here user will enter 1st number. -->   
  69. Number 1: <input type="text" name="number1">   
  70.    
  71.   
  72. <!-- Here user will enter 2nd number. -->   
  73. Number 2: <input type="text" name="number2">   
  74.   
  75.   
  76. <!-- Here result will be displayed. -->   
  77. Get Result: <input type="text" name="total">   
  78.   
  79.   
  80. <!-- Here respective button when clicked, calls only respective artimetic function. -->   
  81. <input type="button" value="ADD" onclick="javascript:addition();">  
  82. <input type="button" value="SUB" onclick="javascript:subtraction();">  
  83. <input type="button" value="MUL" onclick="javascript:multiply();">  
  84. <input type="button" value="DIV" onclick="javascript:division();">  
  85. <input type="button" value="MOD" onclick="javascript:modulus();">  
  86.   
  87. </form>  
  88.   
  89. </body>  
  90. </html>  

Download JavaScript Calculator HTML Code Here : JavaScript Calculator!

square JavaScript Output - Perform Arithmetic Operations


Copy and paste above HTML Code in a notepad and save it as JavaScript-Calculator.html. Now open this saved HTML file in any web browser (e.g: Internet Explorer with ActiveX Enabled) to get result something like this...

Javascript Arithmetic Operations

"Thanks! For reading my JavaScript Tutorial. Enjoy Scripting"...

No comments:

Post a Comment