Programming Projects

Welcome to the Land of Programming Projects

This site hosts projects related to C, C++, .NET, PHP, MYSQL, Javascript and other programming languages.


Project Title: A Simple Calculator In Javascript

Description: This is a simple calculator project written in javascript (and html, ofcourse). Sorry to say, but the design of the calculator is not so attractive because the focus is on the behaviour. i.e : javascript. Copy the calculator code, save in notepad or any other editor that you like with .html extension and run it in you browser.

Difficulty: Easy/Medium

Language: HTML, Javascript (js)

Compiler: NA

View Source

 
 
<HTML> 
<HEAD> 
        <TITLE>A Simple calculator</TITLE> 
 
<script language = javascript> 
var oper=""
var num =""
 
function displaynum(n) {
document.form1.t1.value = document.form1.t1.value + n
}
 
function operator(op) {         
oper = op
 num= document.form1.t1.value
document.form1.t1.value = ""
}
 //code for equals starts here
function equals() {
doesthejob( eval(num) , eval(document.form1.t1.value ), oper)
}
 //a sub-function of equals 
function doesthejob(n1,n2, op) {
if (op == "+" ) 
document.form1.t1.value = n1 + n2
else if ( op == "-" )
document.form1.t1.value = n1- n2
else if (op == "*")
document.form1.t1.value = n1 * n2
else if (op =="/")
document.form1.t1.value = n1/n2 
else if (op=="nCr" )
document.form1.t1.value = fact2(n1)/ fact2(n1 - n2) / fact2(n2)
else if (op =="nPr")
document.form1.t1.value = fact2(n1) / fact2(n1-n2)
}
 //code for equals ends here

function fact2(n) {    // fact2() for nCr & nPr
if (errorchecking(n) ==false)  
return

var answer = 1
for (i = n; i >=2; i--){
answer = answer*i
} 
return answer
}

function fact() {
n = Number(document.form1.t1.value)
if (errorchecking(n) ==false) 
return
var answer = 1
for (i = n; i >=2; i--){
answer = answer*i
} 
document.form1.t1.value = answer
}

function errorchecking(n) {
if ( n < 0) {
alert("Number shouldn't be negative" )
return false 
}
var mod = n%1
if (!mod ==0) {
alert("The number should be an integer" )
return false
}
} 

function prime(n) {
if (errorchecking(n) == false)
return
var b = true
for ( i = 2; i<= n/2; i ++ ) {
if (n % i == 0 ) {
document.form1.t1.value = "Not prime; its first divided by " + i
b = false
break
}
}
if (b)
document.form1.t1.value = "Is prime"
}

function negation() {
document.form1.t1.value = document.form1.t1.value * -1
}
 
function reset() {
document.form1.t1.value = ""
num = ""
}
</script> 
 
</HEAD> 
<BODY> 
<form name = form1> 
<CENTER><input type = text name = t1  value = "" size = 50><p> 
<input type = button value = "   1   " name = b1 onclick = displaynum(1)> 
<input type = button value = "   2   " name = b2 onclick = displaynum(2)> 
<input type = button value = "   3   " name = b3 onclick = displaynum(3)> 
<input type = button value = "   +   " name = bplus onclick = operator("+") 
><br> 

<input type = button value = "   4   " name = b4 onclick = displaynum(4)> 
<input type = button value = "   5   " name = b5 onclick = displaynum(5)> 
<input type = button value = "   6   " name = b6 onclick = displaynum(6)> 
<input type = button value = "   -   " name = bminus  onclick = operator("-")
> <br> 

<input type = button value = "   7   " name = b7 onclick = displaynum(7)> 
<input type = button value = "   8   " name = b8 onclick = displaynum(8)> 
<input type = button value = "   9   " name = b9 onclick = displaynum(9)> 
<input type = button value = "   *   " name = bmultiply  
onclick = operator("*")> <br> 

<input type = button value = "   .   " name = bdot onclick = displaynum(".")> 
<input type = button value = "   0   " name = b0 onclick = displaynum(0)> 
<input type = button value = "   =   "  name= bequal onclick = equals()> 
<input type = button value = "   /   " name = bdivide onclick = operator("/")
> <br> 

<input type = button value = "  -+ " name = bnegate onclick = negation()> 
<input type = button value = "clear" name = bclear onclick = reset()> 
<input type = button value = "  !  " name = bfact onclick =fact() > 
<input type = button value = " nCr " name = bcombination 
onclick = operator("nCr")> 

<input type = button value = " nPr " name = bpermutation 
onclick = operator("nPr")><BR>
 
<input type = button value = "Prime" name = bprime onclick = prime(t1.value)
> 
</CENTER> 
 
</form> 
</BODY> 
</HTML> 
 
 
	   


Notes: The calculator displays a variety of buttons to work with. For each of the buttons from 0-9, the calculator calls the function "displaynum" that displays the corresponding number in the textbox. For each of the operators (+,- etc), it calls "operator" function that notes down the operator and clears the textbox. When equal (=) button is pressed, the calculator calls the function "equals" that produces the desired result based on the operator provided.

Note that the buttons "fact (!) " and "prime" works with one number only and hence there is no need of pressing the "=" button; simply write a number and press these buttons. If you dont understand how "fact" and "prime" works, check out our projects Factorial of a number and IsPrime respectively.