Javascript-Calculator With Numeric Keyboard
Javascript-Calculator With Numeric Keyboard
calc.html
---------------
<html>
<head>
<script type="text/javascript" src="calc.js">
</script>
<link rel=stylesheet href="calc.css">
</head>
<body>
<div id=st align=center>
<form>
<table>
<tr>
<td colspan="4"><div id=top align=center>
<b>Javascript Calculator By Anandhu Arjunan</b>
</div></td>
</tr>
<tr>
<td colspan="4"><input type="number" id=num></td>
</tr>
<tr>
<td><input type=button id=nums value=1 name=- onclick="assN(1)"></td>
<td ><input type=button id=nums value=2 onclick="assN(2)"></td><td ><input type=button value=3 onclick="assN(3)" id=nums></td>
<td><input type=button id=ope value=+ name=+ onclick="addition()"></td>
</tr>
<tr>
<td ><input type=button value=4 id=nums onclick="assN(4)"></td><td ><input type=button value=5 name=- onclick="assN(5)" id=nums></td>
<td ><input type=button value=6 id=nums onclick="assN(6)"></td> <td ><input type=button id=ope value=- name=- onclick="subtraction()"></td>
</tr>
<tr>
<td ><input type=button id=nums value=7 onclick="assN(7)"></td>
<td ><input type=button id=nums value=8 onclick="assN(8)"></td>
<td ><input type=button id=nums value=9 onclick="assN(9)"></td>
<td><input type=button value=* id=ope name=* onclick="multiplication()"></td>
</tr>
<tr>
<td><input type=button value=n! onclick="fact()"></td>
<td><input type=button id=nums value=0 onclick="assN(0)"></td>
<td><input type=button value=c onclick="clr()"></td><td><input type=button id=ope value=/ name=/ onclick="division()"/></td>
</tr>
<tr>
<td><input type=button value=^ onclick="expon()"></td>
<td><input type=button value=sqrt onclick="sqrtf()"></td>
<td><input type=button value=pi onclick="assN(Math.PI)"></td> <td><input type=button value=% id=ope onclick="remainder()"></td>
</tr>
<tr>
<td colspan="4"><input type="button" id="eq" value="=" onclick="findq()"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
calc.js
----------------
var num1,num2,sign,temp;
function addition(){
clrnm2();
num1=document.getElementById('num').value;
document.getElementById('num').value="";
sign="+";
document.getElementById('num').focus();
} function subtraction(){
clrnm2();
num1=document.getElementById('num').value;
document.getElementById('num').value="";
sign="-";
document.getElementById('num').focus();
} function multiplication(){
clrnm2();
num1=document.getElementById('num').value;
document.getElementById('num').value="";
sign="*";
document.getElementById('num').focus();
} function division(){
clrnm2();
num1=document.getElementById('num').value;
document.getElementById('num').value="";
sign="/";
document.getElementById('num').focus();
}
function remainder(){
clrnm2();
num1=document.getElementById('num').value;
document.getElementById('num').value="";
sign="%";
document.getElementById('num').focus();
}
function expon(){
clrnm2();
num1=document.getElementById('num').value;
document.getElementById('num').value="";
sign="^";
document.getElementById('num').focus();
}
function fact(){
clrnm2();
num1=document.getElementById('num').value;
ans=factorial();
num1=ans;
document.getElementById('num').value=ans;
}
function findq(){
if(num2==null){
num2=document.getElementById('num').value;
}
if(sign=="+"){
var ans=parseFloat(num1)+parseFloat(num2);
num1=ans;
document.getElementById('num').value=ans;
}
else if(sign=="-"){
var ans=parseFloat(num1)-parseFloat(num2);
num1=ans;
document.getElementById('num').value=ans;
}
else if(sign=="*"){
var ans=parseFloat(num1)*parseFloat(num2);
num1=ans;
document.getElementById('num').value=ans;
}
else if(sign=="/"){
if(document.getElementById('num').value=="0"){
alert("Cannot Divid By Zero");
document.getElementById('num').value="";
}
else{
var ans=parseFloat(num1)/parseFloat(num2);
num1=ans;
document.getElementById('num').value=ans;
}
}
else if(sign=="%"){
var ans=parseFloat(num1)%parseFloat(num2);
num1=ans;
document.getElementById('num').value=ans;
}
else if(sign=="^"){
var ans=parseInt(Math.pow(parseFloat(num1),parseFloat(num2)));
num1=ans;
document.getElementById('num').value=ans;
}
}
function factorial(){
var as=parseInt(num1);
var fact=1;
var int=1;
for(i=1;i<=as;i++){
fact=fact*i;
}
return fact;
}
function clrnm2(){
if(num2==null){
}
else{
num2=null;
}
return true;
}
function clr(){
num1=num2=sign=temp="";
document.getElementById('num').value="";
}
function assN(n){
document.getElementById('num').value=document.getElementById('num').value+n;
}
function sqrtf(){
document.getElementById('num').value=Math.sqrt(document.getElementById('num').value);
}
calc.css
--------------
body{
font-family: sans-serif;
}
input[type='number']{
width: 509px;
height: 50px;
font-size: 20px;
border:3px solid black;
border-radius: 3px;
}
#eq{
width: 509px;
}
input[type='button']{
width:125px;
height: 45px;
background-color: black;
color: white;
font-size: 25px;
}
#st{
margin-top: 50px;
}
#nums{
background-color: dodgerblue;
color: black; font-size: 25px;
}
#ope{
background-color: deeppink;
color: black;
font-size: 25px;
}
#top{;
padding:20px;
background-color: darkolivegreen;
color:white;
}
#eq{
background-color: white;
color: black;
border: 2px inset black;
font-size: 30px;
}
#eq:hover{
background-color: black;
color: white;
font-size: 30px;
border: 2px inset white;
}
Output
-----------------------
body{
font-family: sans-serif;
}
input[type='number']{
width: 509px;
height: 50px;
font-size: 20px;
border:3px solid black;
border-radius: 3px;
}
#eq{
width: 509px;
}
input[type='button']{
width:125px;
height: 45px;
background-color: black;
color: white;
font-size: 25px;
}
#st{
margin-top: 50px;
}
#nums{
background-color: dodgerblue;
color: black; font-size: 25px;
}
#ope{
background-color: deeppink;
color: black;
font-size: 25px;
}
#top{;
padding:20px;
background-color: darkolivegreen;
color:white;
}
#eq{
background-color: white;
color: black;
border: 2px inset black;
font-size: 30px;
}
#eq:hover{
background-color: black;
color: white;
font-size: 30px;
border: 2px inset white;
}
Comments
Post a Comment