Scientific Calculator Source Code

Scientific Calculator Source Code
Code Snippet:Scientific Calculator
Author: Brandon Yen
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 13,635
License: MIT
Edit Code online: View on CodePen
Read More

Yet another scientific calculator source code that’s written in HTML, CSS, and Vanilla JavaScript. This calculator comes with a clean design with a dark theme. Along with the basic calculations, it allows users to find out the values of sin, cos, tan, log, In, factorial, exp, radians, and degrees.

You can integrate this calculator into your web/app project to allow users to solve scientific problems quickly. The theme and styles can be highly customized with additional CSS according to your needs.

How to Create Scientific Calculator

1. In order to create a scientific calculator, create a div element with a class name "calculator". Likewise, create a div element with a class name "display" and place an input element inside it with id “output”. Similarly, create div elements for buttons, wrap all these elements into a div tag and define its class name "container". So, the complete HTML structure for the scientific calculator is as follows:

<div id="container">
    <div class="calculator">
      <div class="display">
        <input id="output" type="text" name="" disabled/>
      </div>
      <div id="buttons">
        
        <div class="row">
          <div class="button" id="clear">C</div>
          <div class="button">Ï€</div>
          <div class="button">sqrt</div>
          <div class="button">sq</div>
          <div class="button">%</div>
          <div class="button" id="backspace"><=</div>
        </div>
        <div class="row">
          <div class="button">log</div>
          <div class="button">sin</div>
          <div class="button">exp</div>
          <div class="button">^</div>
          <div class="button">+/-</div>
          <div class="button">÷</div>
        </div>
        <div class="row">
          <div class="button">ln</div>
          <div class="button">cos</div>
          <div class="button number">7</div>
          <div class="button number">8</div>
          <div class="button number">9</div>
          <div class="button">x</div>
        </div>
        <div class="row">
          <div class="button">n!</div>
          <div class="button">tan</div>
          <div class="button number">4</div>
          <div class="button number">5</div>
          <div class="button number">6</div>
          <div class="button">-</div>
        </div>
        <div class="row">
          <div class="button" id="radians">radians</div>
          <div class="button number">1</div>
          <div class="button number">2</div>
          <div class="button number">3</div>
          <div class="button">+</div>
        </div>
        <div class="row">
          <div class="button" id="degrees">degrees</div>
          <div class="button number" id="zero">0</div>
          <div class="button" id>.</div>
          <div class="button" id="equals">=</div>
        </div>
      </div>
    </div>
</div>

2. After creating the HTML structure, now style the calculator using CSS. You can set the custom styles in order to customize the calculator interface. Therefore, the following are the basic CSS styles for the scientific calculator.

.calculator{
    max-width: 612px;
    margin: 10px auto;
}
#output {
  background-color: #000;
  color: #fff;
  width: 100%;
  height: 80px;
  font-size: 48px;
  border-radius: 5px;
  text-align: left;
  padding: 0px 10px 0px 10px;
  box-shadow: 0px 0px 30px -5px #fff;
  box-sizing: border-box;
}
#buttons {
  margin-top: 15px;
}
.button {
  color: #fff;
  text-align: center;
  line-height: 70px;
  display: inline-block;
  background-color: #595959;
  border: 1px solid #33ccff;
  height: 70px;
  width: 100px;
  font-size: 36px;
  margin-right: -4.6px;
  cursor: pointer;
}
.button:hover {
  background-color: #0066ff;
  box-shadow: 0px 0px 30px 0px #fff;
}
.button:active {
  background-color: #80b3ff;
  box-shadow: 0px 0px 30px 0px #000;
}
.number {
  background-color: #404040;
}
.number:active {
  background-color: #80b3ff;
  box-shadow: 0px 0px 30px 0px #000;
}
#zero {
  width: 200px;
}
#decimal {
  font-weight: bold;
}
#equals {
  background-color: #0066ff;
}
#equals:hover {
  background-color: #fff;
  color: #000;
  box-shadow: 0px 0px 30px -5px #fff;
}
#backspace {
  background-color: #333333;
}
#backspace:hover {
  background-color: #99003d;
}
#backspace:active {
  background-color: #ff4d94;
  box-shadow: 0px 0px 30px 0px #000;
}
#clear {
  background-color: #262626;
}
#clear:hover {
  background-color: #cc0000;
}
#clear:active {
  background-color: #ff4d4d;
  box-shadow: 0px 0px 30px 0px #000;
}
#radians, #degrees {
  width: 200px;
}
#radians:hover {
  background-color: #00b38f;
}
#degrees:hover {
  background-color: #00b38f;
}

3. Finally, add the following JavaScript scientific calculator program before closing the body tag and done.

var display = document.getElementById("output");
var buttons = document.getElementsByClassName("button");

Array.prototype.forEach.call(buttons, function(button) {
  button.addEventListener("click", function() {
    if (button.textContent != "=" && button.textContent != "C" && button.textContent != "x" && button.textContent != "÷" && button.textContent != "sqrt" && button.textContent != "sq" && button.textContent != "%" && button.textContent != "<=" && button.textContent != "+/-" && button.textContent != "sin" && button.textContent != "cos" && button.textContent != "tan" && button.textContent != "log" && button.textContent != "ln" && button.textContent != "^" && button.textContent != "n!" && button.textContent != "π" && button.textContent != "exp" && button.textContent != "radians" && button.textContent != "degrees") {
      display.value += button.textContent;
    } else if (button.textContent === "=") {
      equals();
    } else if (button.textContent === "C") {
      clear();
    } else if (button.textContent === "x") {
      multiply();
    } else if (button.textContent === "÷") {
      divide();
    } else if (button.textContent === "+/-") {
      plusMinus();
    } else if (button.textContent === "<=") {
      backspace();
    } else if (button.textContent === "%") {
      percent();
    } else if (button.textContent === "Ï€") {
      pi();
    } else if (button.textContent === "sq") {
      square();
    } else if (button.textContent === "sqrt") {
      squareRoot();
    } else if (button.textContent === "sin") {
      sin();
    } else if (button.textContent === "cos") {
      cos();
    } else if (button.textContent === "tan") {
      tan();
    } else if (button.textContent === "log") {
      log();
    } else if (button.textContent === "ln") {
      ln();
    } else if (button.textContent === "^") {
      exponent();
    } else if (button.textContent === "n!") {
      factorial();
    } else if (button.textContent === "exp") {
      exp();
    } else if (button.textContent === "radians") {
      radians();
    } else if (button.textContent === "degrees") {
      degrees();
    }
  });
});

// function nextLine() {
//   if(display.value.length > 19) {
//     document.write("\n");
//   }
// }
// ***button functions***
function checkLength() {
  if (display.value.length >= 23) {
    display.value = "Overload Error";
  }
}
function syntaxError() {
  if (eval(display.value) == SyntaxError) {
    display.value = "Syntax Error";
  }
}
function equals() {
  if ((display.value).indexOf("^") > -1) {
    var base = (display.value).slice(0, (display.value).indexOf("^"));
    var exponent = (display.value).slice((display.value).indexOf("^") + 1);
    display.value = eval("Math.pow(" + base + "," + exponent + ")");
  } else {
    display.value = eval(display.value);
    checkLength();
    syntaxError();
  }
}
function clear() {
  display.value = "";
}
function backspace() {
  display.value = display.value.substring(0, display.value.length - 1);
}
function multiply() {
  display.value = display.value + "*";
}
function divide() {
  display.value = display.value + "/";
}
function plusMinus() {
  if (display.value.charAt(0) === "-") {
    display.value = display.value.slice(1);
  } else {
    display.value = "-" + display.value;
  }
}
function factorial() {
  var result = 1;
  if (display.value === 0) {
    display.value = "1";
  } else if (display.value < 0) {
    display.value = "undefined";
  } else {
    var result = 1;
    for (var i = display.value; i > 0; i--) {
      result = result * i;
    }
    display.value = result;
  }
}
function pi() {
  // if(display.value === "") {
  //   display.value = Math.PI;
  // }
  display.value = (display.value * Math.PI);
}
function square() {
  display.value = eval(display.value * display.value);
}
function squareRoot() {
  display.value = Math.sqrt(display.value);
}
function percent() {
  display.value = display.value / 100;
}
function sin() {
  display.value = Math.sin(display.value);
}
function cos() {
  display.value = Math.cos(display.value);
}
function tan() {
  display.value = Math.tan(display.value);
}
function log() {
  display.value = Math.log10(display.value);
}
function ln() {
  display.value = Math.log(display.value);
}
function exponent() {
  display.value = display.value + "^";
}
function exp() {
  display.value = Math.exp(display.value);
}
function radians() {
  display.value = display.value * (Math.PI / 180);
}
function degrees() {
  display.value = display.value * (180 / Math.PI);
}

That’s all! hopefully, you have successfully integrated this scientific calculator source code into your project. If you have any questions or suggestions, let me know by comment below.

2 thoughts on “Scientific Calculator Source Code”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X