Related Articles

HTML | Scientific Calculator

• Difficulty Level : Expert
• Last Updated : 22 Jun, 2021

Here a Scientific Calculator is going to be formed by using HTML, CSS, and JavaScript.

• Calculator Title: This is the title at the top of our application, “GeeksforGeeks Scientific Calculator”.
• Output Screen: This will be output screen, where all text will be shown. Like the input that the user will type and the answer calculated from the user input. So, we can again break down this into two smaller pieces as shown below:
• Question Output: This will be the input given by the user.
• Answer Output: This will be the result calculated from user input.

Operations: This scientific calculator can perform following operations which are listed below:

• Square root
• Percentage
• Factorial
• Constants (pie, euler constant, log2 base e, log 10 base e)
• Exponent
• log base 2, 10, e
• Power
• Sin, Tan, Cos (enter angle in radians )

HTML Code:

HTML

 ```<``html``>``    ` `<``head``>``    ``<``title``>``        ``Scientific Calculator using HTML, CSS and Js``    ````    ` `    ````    ``<``style``>``    ` `        ``/* Style to set the title of calculator */``        ``.title {``            ``margin-bottom: 10px;``            ``padding: 5px 0;``            ``font-size: 20px;``            ``font-weight:bold;``            ``text-align:center;``            ``width: 447px;``            ``color:green;``            ``border: solid black 2px;``        ``}``        ` `        ``/* Set the button style */``        ``#btn {``            ``width: 100%;``            ``height: 40px;``            ``font-size: 30px;``        ``}``        ` `        ``input[type="button"] {``            ``background-color:green;``            ``color: black;``            ``border: solid black 2px;``            ``width:100%``        ``}``        ` `        ``/* Set input textarea */``        ``input[type="text"] {``            ``background-color:white;``            ``border: solid black 2px;``            ``width:100%``        ``}``    ````    ` `    ``<``script``>``        ` `        ``/* Creating function in HTML for backspace operation */``        ``function backspace(calc) {                                            ``            ``size = calc.display.value.length;``            ``calc.display.value = calc.display.value.substring(0, size-1);``        ``}``        ` `        ``/* Creating function to calculate factorial of element */``        ``function calculate(calc) {``            ` `            ``/* Check if function include ! character then``            ``calculate factorial of number */``            ``if(calc.display.value.includes("!")) {``                ` `                ``size = calc.display.value.length;``                ``n = Number(calc.display.value.substring(0, size-1));``                ``f = 1;``                ` `                ``for(i = 2; i <= n; i++)``                    ``f = f*i;``                ``calc.display.value = f;``            ``}``            ` `            ``/* If function include % character then calculate``            ``the % of number */``            ``else if(calc.display.value.includes("%")) {``                ` `                ``size = calc.display.value.length;``                ``n = Number(calc.display.value.substring(0, size-1));``                ``calc.display.value = n/100;``            ``}` `            ``else   ``                ``/* Otherwise evaluate and execute output */``                ``calc.display.value = eval(calc.display.value);``        ``}``    ````` `<``body``>``    ``<``div` `class` `= ``title` `>``        ``GeeksforGeeks Scientific Calculator``    ````    ` `    ``<``form` `name` `= ``"calc"``>``        ` `    ``<``table` `id` `= ``"calc"` `border` `= ``2``>``        ` `    ``<``tr``>``        ``<``td` `colspan``=``5``><``input` `id``=``"btn"` `name``=``"display"``            ``onkeypress="return event.charCode >= 48``            ``&& event.charCode <= 57" type="text">``        ````    ````    ` `    ``<``tr``>``        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"1"``                ``OnClick``=``"calc.display.value+='1'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"2"``                ``OnClick``=``"calc.display.value+='2'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"3"``                ``OnClick``=``"calc.display.value+='3'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"C"``                ``OnClick``=``"calc.display.value=''"``>``        ````        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"<-"``                ``OnClick``=``"backspace(this.form)"``>``        ````        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"="``                ``OnClick``=``"calculate(this.form)"``>``        ````    ````    ` `    ``<``tr``>``        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"4"``                ``OnClick``=``"calc.display.value+='4'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"5"``                ``OnClick``=``"calc.display.value+='5'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"6"``                ``OnClick``=``"calc.display.value+='6'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"-"``                ``OnClick``=``"calc.display.value='-'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"%"``                ``OnClick``=``"calc.display.value+='%'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"cos"``                ``OnClick``=``"calc.display.value='Math.cos('"``>``        ````    ````    ` `    ``<``tr``>``        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"7"``                ``OnClick``=``"calc.display.value+='7'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"8"``                ``OnClick``=``"calc.display.value+='8'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"9"``                ``OnClick``=``"calc.display.value+='9'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"*"``                ``OnClick``=``"calc.display.value+='*'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"n!"``                ``OnClick``=``"calc.display.value+='!'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"sin"``                ``OnClick``=``"calc.display.value='Math.sin('"``>``        ````    ````    ` `    ``<``tr``>``        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"."``                ``OnClick``=``"calc.display.value+='.'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"0"``                ``OnClick``=``"calc.display.value+='0'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``","``                ``OnClick``=``"calc.display.value+=','"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"+"``                ``OnClick``=``"calc.display.value+='+'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"/"``                ``OnClick``=``"calc.display.value+='/'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"tan"``                ``OnClick``=``"calc.display.value='Math.tan('"``>``        ````    ````    ` `    ``<``tr``>``        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"E"``                ``OnClick``=``"calc.display.value+='Math.E'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"pi"``                ``OnClick``=``"calc.display.value+='Math.PI'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"^"``                ``OnClick``=``"calc.display.value+='Math.pow('"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"("``                ``OnClick``=``"calc.display.value+='('"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``")"``                ``OnClick``=``"calc.display.value+=')'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"log"``                ``OnClick``=``"calc.display.value='Math.log('"``>``        ````    ````    ` `    ``<``tr``>``        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"sqrt"``                ``OnClick``=``"calc.display.value+='Math.sqrt('"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"ln2"``                ``OnClick``=``"calc.display.value+='Math.LN2'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"ln10"``                ``OnClick``=``"calc.display.value+='Math.Log10'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"l2e"``                ``OnClick``=``"calc.display.value+='Math.LOG2E'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"l10e"``                ``OnClick``=``"calc.display.value+='Math.log10'"``>``        ````        ` `        ``<``td``><``input` `id``=``"btn"` `type``=``"button"` `value``=``"exp"``                ``OnClick``=``"calc.display.value='Math.exp('"``>``        ````    ````    ````    ````` `                               `

Generated Scientific Calculator:

Calculate cos(0) Value:

Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up