# Simple Math Calculator Using CheckBox

This is a simple program using JavaScript. And it’s called Simple Math Calculator Using CheckBox. This is a very common program, but this is suited for the beginner in JavaScript Language.

This program contains 2 textboxes where the user types for the numeric value, then 4 check box for addition, subtraction, multiplication, and division. And we have 1 button to compute and 1 button for clearing the value in the textbox to solve another numeric value.

## Math Operators

2. Subtraction
3. Multiplication
4. Division

HTML Form

This form field contains textboxes, checkboxes, and buttons to operate this program.

`<form action="" method="POST"><table cellpadding="5" cellspacing="5" style="text-align:center;" width="100%" border="0">	<tr>		<td><label>First Value</label></td>		<td><input type="number" name="first_TextBox_Value" id="number1_TextBox" autofocus="autofocus"></td>	</tr>	<tr>		<td><label>Second Value</label></td>		<td><input type="number" name="second_TextBox_Value" id="number2_TextBox"></td>	</tr></table><h2>Math Operators</h2><table cellpadding="5" cellspacing="5" style="text-align:center;" width="100%" border="0">	<tr>		<td><input type="checkbox" name="math_Operators" onclick="reset_Operators();" />   <label style="color:red;">Addition</label></td>		<td><input type="checkbox" name="math_Operators" onclick="reset_Operators();" />   <label style="color:red;">Subtraction</label></td>	</tr>	<tr>		<td><input type="checkbox" name="math_Operators" onclick="reset_Operators();" />   <label style="color:red;">Multiplication</label></td>		<td><input type="checkbox" name="math_Operators" onclick="reset_Operators();" />   <label style="color:red;">Division</label></td>	</tr></table><br /><br />           <input type="submit" value="Solve" onclick="return solve();" /><input type="submit" value="Clear" onclick="return clear();" /><br /><br /><div id="math_Results"></div></form>`

JavaScript Code

This script will execute after the user controls the form field.

`<script>function solve() { var operators_If = document.getElementsByName("math_Operators");var first_TextBox_Value = document.getElementById("number1_TextBox").value;var no_two = document.getElementById("number2_TextBox").value;  if (first_TextBox_Value=="") {var math_Results = "<span class='warning'>Enter the first value.</span>";document.getElementById('math_Results').innerHTML = math_Results;return false;} else if (no_two=="") {var math_Results = "<span class='warning'>Enter the second value.</span>";document.getElementById('math_Results').innerHTML = math_Results;return false;} if (operators_If[0].checked == true) {sum = parseInt(first_TextBox_Value) + parseInt(no_two);							results = "<span class='message'> The sum of " + first_TextBox_Value + " and " + no_two + " is <b style='color:blue;'>" + sum +"</b>.</span>";document.getElementById('math_Results').innerHTML = results;return false;} else if (operators_If[1].checked == true) { diff = parseInt(first_TextBox_Value) - parseInt(no_two);					results = "<span class='message'> The difference between " + first_TextBox_Value + " and " + no_two + " is <b style='color:blue;'>" + diff +"</b>.</span>";document.getElementById('math_Results').innerHTML = results;return false;} else if (operators_If[2].checked == true) { product = parseInt(first_TextBox_Value) *parseInt(no_two);					results = "<span class='message'>The product between " + first_TextBox_Value + " and " + no_two + " is <b style='color:blue;'>" + product +"</b>.</span>";document.getElementById('math_Results').innerHTML = results;return false;} else if (operators_If[3].checked == true) { quotient = parseInt(first_TextBox_Value) / parseInt(no_two);results = "<span class='message'>The quotient between " + first_TextBox_Value + " and " + no_two + " is <b style='color:blue;'>" + quotient +"</b>. </span>";document.getElementById('math_Results').innerHTML = results;return false;} else {var math_Results = "<span class='warning'>Select your math operators.</span>";document.getElementById('math_Results').innerHTML = math_Results;return false;}return true;}function reset_Operators() {document.getElementById('math_Results').innerHTML = '';}function clear(){document.getElementById('math_Results').innerHTML = "";first_TextBox_Value="";no_two="";first_TextBox_Value.focus();}</script>`

And, this is our style for the layout of a form field.

`<style type="text/css">body{	width:500px;	margin:auto;	border:blue 1px solid;	text-align:center;}h1 {	color:blue;}h2 {	color:red;}label {    font-size: 20px;    font-weight: bolder;    font-family: cursive;    color: blue;}input[type="number"] {    font-size: 18px;    border: blue 1px solid;    text-indent: 5px;    width: 150px;}input[type="submit"] {    font-size: 18px;    color: blue;    background: azure;    border: blue 1px solid;    padding: 6px;    border-radius: 4px;	cursor:pointer;}#math_Results{	font-size: 23px;    font-weight: bold;    font-family: cursive;    color: red;}</style>`

## Output:

For Subtraction Operator.

For Multiplication Operator.

For Division Operator.

So, this is it, you have Simple Math Calculator Using CheckBox in JavaScript. Kindly click the "Download Code" button for full source code. Enjoy Coding. Hope you can learn from this.

So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.