How to Create a Binary Calculator using HTML, CSS and JavaScript?


A binary calculator is a program that performs mathematical calculation on binary numbers. Now, as of you remember the binary numbers are those numbers which are formed from only two numbers i.e., 0 and 1. In this blog, we will use this program to calculate addition, subtraction, multiplication and division on binary numbers. This will be a basic calculator that will use basic concepts of HTML, CSS and JavaScript to do the same. So, let’s get started with the HTML structure.

HTML Structure

Firstly, we will make a form which will be divided into table rows giving features like adding 1, adding 0, clearing the display and buttons of plus, minus, multiply and divide sign with a equal to sign.

<form>
   <table>
      <tr>
         <td colspan="4">
            <input type="text" id="display" disabled />
         </td>
      </tr>
      <tr>
         <td>
            <input type="button" value="1" onclick="addToDisplay(1)" />
         </td>
         <td>
            <input type="button" value="0" onclick="addToDisplay(0)" />
         </td>
         <td>
            <input type="button" value="C" onclick="clearDisplay()" />
         </td>
         <td>
            <input type="button" value="+" onclick="addToDisplay('+')" />
         </td>
         <td>
            <input type="button" value="-" onclick="addToDisplay('-')" />
         </td>
         <td>
            <input type="button" value="*" onclick="addToDisplay('*')" />
         </td>
         <td>
            <input type="button" value="/" onclick="addToDisplay('/')" />
         </td>
         <td>
            <input type="button" value="=" onclick="calculate()" />
         </td>
      </tr>
      <tr>
         <td colspan="4">
            Equivalent Decimal is:
            <p id="toDecimal"></p>
         </td>
      </tr>
      <tr>
         <td colspan="4">
            <p id="previousCalculation"></p>
         </td>
      </tr>
      <!-- more buttons for the other operations -->
   </table>
</form>

As you can see, we have an input field with the id "display" which is disabled. This field will be used to display the input and the result of the calculation. We also have a set of buttons for the different binary digits (0 and 1) and the different mathematical operations (+,-,*,/). Each button has an onclick attribute that triggers a JavaScript function when clicked.

CSS Styling

Next, we will add some CSS styling to make our calculator look more presentable.

<style>
   /* Center the calculator on the page */
   table {
      margin: 0 auto;
      padding: 20px;
   }
   /* Style the display */
   #display {
      background-color: #f2f2f2; /* gray */
      text-align: right;
      padding: 12px 20px;
      font-size: 20px;
      border: none;
      width: 100%;
   }
   /* Add some spacing between the buttons */
   input[type="button"] {
      margin: 5px;
   }
   /* Give the buttons a consistent size and appearance */
   input[type="button"] {
      width: 50px;
      height: 50px;
      font-size: 18px;
      background-color: #f2f2f2;
      border: none;
      cursor: pointer;
   }
   #toDecimal {
      font-size: 30px;
   }
   /* Add hover effect to the buttons */
   input[type="button"]:hover {
      background-color: #e6e6e6;
   }
   /* Add a different style for the operator buttons */
   input[type="button"][value="+"],
   input[type="button"][value="-"],
   input[type="button"][value="*"],
   input[type="button"][value="/"] {
      background-color: #4caf50;
      color: white;
   }
   /* Add a different style for the clear button */
   input[type="button"][value="C"] {
      background-color: #f44336;
      color: white;
   }
   /* Add a different style for the equal button */
   input[type="button"][value="="] {
      background-color: #2196f3;
      color: white;
   }
</style>

JavaScript Functionality

Finally, we will add the JavaScript functionality to our calculator.

<script>
   function addToDisplay(val) {
      var display = document.getElementById("display");
      display.value += val;
   }
   function clearDisplay() {
      var display = document.getElementById("display");
      display.value = "";
      document.getElementById("toDecimal").innerHTML = "";
   }
   function calculate() {
      var display = document.getElementById("display");
      var result = eval(display.value);
      display.value = result;
      var decimalNumber = parseInt(result, 2);
      document.getElementById("toDecimal").innerHTML = decimalNumber;
   }
   function calculate() {
      var display = document.getElementById("display");
      var input = display.value;
      var result;
 
     //splitting the input by operator
      var numbers = input.split(/[+\-*/]/);
      var operator = input.replace(numbers[0], "").replace(numbers[1], "");
      
      //converting strings to binary
      var num1 = parseInt(numbers[0], 2);
      var num2 = parseInt(numbers[1], 2);
      
      //checking the operator and performing the corresponding operation
      switch (operator) {
         case "+":
         result = (num1 + num2).toString(2);
         var decimalNumber = parseInt(result, 2);
         document.getElementById("toDecimal").innerHTML = decimalNumber;
         break;
         case "-":
         result = (num1 - num2).toString(2);
         var decimalNumber = parseInt(result, 2);
         document.getElementById("toDecimal").innerHTML = decimalNumber;
         break;
         case "*":
         result = (num1 * num2).toString(2);
         var decimalNumber = parseInt(result, 2);
         document.getElementById("toDecimal").innerHTML = decimalNumber;
         break;
         case "/":
         result = (num1 / num2).toString(2);
         var decimalNumber = parseInt(result, 2);
         document.getElementById("toDecimal").innerHTML = decimalNumber;
         break;
         default:
         result = "Invalid operator";
         var decimalNumber = parseInt(result, 2);
         document.getElementById("toDecimal").innerHTML = decimalNumber;
      }
      display.value = result;
   }
</script>

Combining all above code in index.html file

<!DOCTYPE html>
<html>
<head>
   <title>Calculator</title> 
      <style>
         /* Center the calculator on the page */
         table {
            margin: 0 auto;
            padding: 20px;
         }
         /* Style the display */
         #display {
            background-color: #f2f2f2; /* gray */
            text-align: right;
            padding: 12px 20px;
            font-size: 20px;
            border: none;
            width: 100%;
         }
         /* Add some spacing between the buttons */
         input[type="button"] {
            margin: 5px;
         }
         /* Give the buttons a consistent size and appearance */
         input[type="button"] {
            width: 50px;
            height: 50px;
            font-size: 18px;
            background-color: #f2f2f2;
            border: none;
            cursor: pointer;
         }
         #toDecimal {
            font-size: 30px;
         }
         /* Add hover effect to the buttons */
         input[type="button"]:hover {
            background-color: #e6e6e6;
         }
         /* Add a different style for the operator buttons */
         input[type="button"][value="+"],
         input[type="button"][value="-"],
         input[type="button"][value="*"],
         input[type="button"][value="/"] {
            background-color: #4caf50;
            color: white;
         }
         /* Add a different style for the clear button */
         input[type="button"][value="C"] {
            background-color: #f44336;
            color: white;
         }
         /* Add a different style for the equal button */
         input[type="button"][value="="] {
            background-color: #2196f3;
            color: white;
         }
   </style>
</head>
<body>
   <form>
      <table>
         <tr>
            <td colspan="4">
               <input type="text" id="display" disabled />
            </td>
         </tr>
         <tr>
            <td>
               <input type="button" value="1" onclick="addToDisplay(1)" />
            </td>
            <td>
               <input type="button" value="0" onclick="addToDisplay(0)" />
            </td>
            <td>
               <input type="button" value="C" onclick="clearDisplay()" />
            </td>
            <td>
               <input type="button" value="+" onclick="addToDisplay('+')" />
            </td>
            <td>
               <input type="button" value="-" onclick="addToDisplay('-')" />
            </td>
            <td>
               <input type="button" value="*" onclick="addToDisplay('*')" />
            </td>
            <td> 
               <input type="button" value="/" onclick="addToDisplay('/')" />
            </td>
            <td>
               <input type="button" value="=" onclick="calculate()" />
            </td>
         </tr>
         <tr>
            <td colspan="4">
               Equivalent Decimal is:
               <p id="toDecimal"></p>
            </td>
         </tr>
         <tr>
            <td colspan="4">
               <p id="previousCalculation"></p>
            </td>
         </tr>
         <!-- more buttons for the other operations -->
      </table>
   </form>
   <script>
      function addToDisplay(val) {
         var display = document.getElementById("display");
         display.value += val;
      }
      function clearDisplay() {
         var display = document.getElementById("display");
         display.value = "";
         document.getElementById("toDecimal").innerHTML = "";
      }
      function calculate() {
         var display = document.getElementById("display");
         var result = eval(display.value);
         display.value = result;
         var decimalNumber = parseInt(result, 2);
         document.getElementById("toDecimal").innerHTML = decimalNumber;
      }
      function calculate() {
         var display = document.getElementById("display");
         var input = display.value;
         var result;
         
         //splitting the input by operator
         var numbers = input.split(/[+\-*/]/);
         var operator = input.replace(numbers[0], "").replace(numbers[1], "");
         
         //converting strings to binary
         var num1 = parseInt(numbers[0], 2);
         var num2 = parseInt(numbers[1], 2);
         
         //checking the operator and performing the corresponding operation
         switch (operator) {
            case "+":
            result = (num1 + num2).toString(2);
            var decimalNumber = parseInt(result, 2);
            document.getElementById("toDecimal").innerHTML = decimalNumber;
            break;
            case "-":
            result = (num1 - num2).toString(2);
            var decimalNumber = parseInt(result, 2);
            document.getElementById("toDecimal").innerHTML = decimalNumber;
            break;
            case "*":
            result = (num1 * num2).toString(2);
            var decimalNumber = parseInt(result, 2);
            document.getElementById("toDecimal").innerHTML = decimalNumber;
            break;
            case "/":
            result = (num1 / num2).toString(2);
            var decimalNumber = parseInt(result, 2);
            document.getElementById("toDecimal").innerHTML = decimalNumber;
            break;
            default:
            result = "Invalid operator";
            var decimalNumber = parseInt(result, 2);
            document.getElementById("toDecimal").innerHTML = decimalNumber;
         }
         display.value = result;
         localStorage.setItem("previousCalculation", input + " = " + result);
         var previousCalculation = localStorage.getItem("previousCalculation");
         document.getElementById("previousCalculation").innerHTML =  previousCalculation;
      }
   </script>
</body>
</html>

In this tutorial, we have learned how to create a binary calculator using HTML, CSS and JavaScript. We have seen how to set up the HTML structure, add CSS styling and JavaScript functionality to create a working calculator. You can add more functionality like handling error cases and adding more operations as per your requirement. This project can help you understand how different languages work together to create a dynamic and interactive web application.

Updated on: 21-Feb-2023

705 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements