Mail your problems to us on lawania@lavania.in, we will solve it and put the solution here at Lawania.com

Simple Calculator in Javascript Code with Working Example ( +, -, /, *, %, Square root, random, sin )

Working Calculator Click to Test

Test Here


<html>
<head>
<title> Calculator </title>
</head>

<body>

<table   border="2">

<tr>
<td colspan="4">

 <input type="text" id="n1">
</td>
</tr>


<tr>

<td width="25%"> <button type="button" onclick="f1(1)"> 1 </button> </td>
<td width="25%"> <button type="button" onclick="f1(2)"> 2 </button> </td>
<td width="25%"> <button type="button" onclick="f1(3)"> 3 </button> </td>
<td width="25%"> <button type="button" onclick="f1(4)"> 4 </button> </td>

</tr>

<tr>

<td width="25%"> <button type="button" onclick="f1(5)"> 5 </button> </td>
<td width="25%"> <button type="button" onclick="f1(6)"> 6 </button> </td>
<td width="25%"> <button type="button" onclick="f1(7)"> 7 </button> </td>
<td width="25%"> <button type="button" onclick="f1(8)"> 8 </button> </td>

</tr>

<tr>

<td width="25%"> <button type="button" onclick="f1(9)"> 9 </button> </td>
<td width="25%"> <button type="button" onclick="f1(0)"> 0 </button> </td>
<td width="25%"> <button type="button" onclick="f1('+')"> + </button> </td>
<td width="25%"> <button type="button" onclick="f1('-')"> - </buton> </td>

</tr>

<tr>

<td width="25%"> <button type="button" onclick="f1('*')"> X </button>   </td>
<td width="25%"> <button type="button" onclick="f1('/')"> / </button> </td>
<td width="25%"> <button type="button" onclick="f1('%')"> % </button> </td>
<td width="25%"> <button type="button" onclick="equal()"> = </button> </td>

</tr>
<tr>

<td width="25%"> <button type="button" onclick="equal('sqrt')"> SQRT </button>   </td>
<td width="25%"> <button type="button" onclick="equal(' ')"> C </button> </td>
<td width="25%"> <button type="button" onclick="equal('rand')"> RAND </button> </td>
<td width="25%"> <button type="button" onclick="equal('sin')"> sin </button> </td>

</tr>


</table>



<script>

function  f1(num)
{
         var disp=document.getElementById("n1").value;

                   disp=disp+num;
                   document.getElementById('n1').value=disp;
}


function  equal(oper)
{

                       switch(oper)
                        {

                         case 'sqrt' : disp = document.getElementById("n1").value;
                                        disp = Math.sqrt(parseInt(disp));
document.getElementById('n1').value=disp;
break;
                         case ' ' :
disp = ' ';
document.getElementById('n1').value=disp;
break;

case 'rand' : //disp = document.getElementById("n1").value;
                                        //disp = Math.sqrt(parseInt(disp));
disp = ' ';
document.getElementById('n1').value=disp;
disp = Math.random();
document.getElementById('n1').value=disp;
break;

case 'sin' : disp = document.getElementById("n1").value;
document.getElementById('n1').value=' ';

disp = parseInt(disp);
disp = Math.sin(disp);
document.getElementById('n1').value=disp;
break;



                         default :


var disp=document.getElementById("n1").value;
                        disp=eval(disp);
      document.getElementById('n1').value=disp;
                        }
                }

</script>
</body>
</html>





Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment