Intellipaat Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
2 views
by (1.5k points)

I have a piece of code for making a Javascript calculator that is not working, is there any alternatives code for that or how do I fix the error?

showresult(choise){
var n1=parsefloat(document.getElementById('num1').value);
var n2=parsefloat(document.getElementById('num2').value);
var r;
var c=choise;

switch(c)
	{
	case '1':
		r=n1+n2;
		break;
	case '2':
		r=n1-n2;
		break;
	case '3':
		r=n1*n2;
		break;
	case '4': 
		r=n1/n2;
		break;
	case '5':
		r=n2*100/n1;
		break;
	default:
		break;
			
	}
document.getElementById('result').innerHTML=r;

	

}
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<script src="calculator.js" type="text/javascript"></script>
</head>
<body>


<h1>My calculator</h1>
<table border="1" cellpadding="5" cellspacing="5" width="600">
<tr align="center">
<td>First number</td>
<td>Second Number</td>
<td>Result</td>
</tr>
<tr align="center">
<td><input name="number1" type="text" size=10 id='num1'/></td>
<td><input name="number2" type="text" size=10 id='num2'/></td>
<td> <input type="text" id='result' readonly ></td>
</tr>
<tr>
<td colspan="3">
<button onclick="showresult('1')">+</button>
<button onclick="showresult('2')">-</button>
<button onclick="showresult('3')">*</button>
<button onclick="showresult('4')">/</button>
<button onclick="showresult('5')">%</button>
</td>
</tr>

</table>
</body>
</html>

 

1 Answer

0 votes
by (1.4k points)

Java is a very high programming language. Because of the diversity java has, it's possible to build almost anything. Let’s make a simple calculator usinjava. 

  • Using the Scanner class, take two numbers

    • Switch case branching is used to execute the section 

    • To compare the corresponding operations, use a switch case 

    • To test the respective operations using a switch event 

Code: 

// Java program for simple calculator  

import java.io.*; // importinnecessary libraries 

import java.lang.*;  // importinnecessary libraries 

import java.lang.Math 

import java.util.Scanner 

public class BasicCalculator {  

public static void main(String[args 

 

// stores two numbers  

double number1, number2;  

// Taking input from the user  

Scanner sc = new Scanner(System.in);  

System.out.println("Please enter the numbers");  

// take the inputs  

number1 = sc.nextDouble();  

number2 = sc.nextDouble();  

System.out.println("Please enter the operator (+,-,*,/)");  

char op = sc.next().charAt(0);  

double o = 0;  

switch (op) {  

// Case to add two numbers  

case '+':  

o = num1 + num2;  

break;  

// Case to subtract two numbers  

case '-':  

o = num1 - num2;  

break;  

// Case to multiply two numbers  

case '*':  

o = num1 * num2;  

break;  

// Case to divide two numbers  

case '/':  

o = num1 / num2;  

break;  

default:  

System.out.println("You enter wrong input");  

break;  

 

System.out.println("The final result:");  

System.out.println();  

// print the final result  

System.out.println(num1 + " " + op + " " + num2+ " = " + o);  

 

} 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...