basic calculator
C++ Program
#include
using namespace std;
int main()
{
double num1, num2;
char op;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> op;
switch (op)
{
case '+':
cout << num1 + num2;
break;
case '-':
cout << num1 - num2;
break;
case '*':
cout << num1 * num2;
break;
case '/':
cout << num1 / num2;
break;
default:
cout << "Error! operator is not correct";
break;
}
return 0;
}
java Program
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double num1, num2;
System.out.print("Enter first number:");
num1 = sc.nextDouble();
System.out.print("Enter second number:");
num2 = sc.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char operator = sc.next().charAt(0);
double output;
switch(operator)
{
case '+':
output = num1 + num2;
break;
case '-':
output = num1 - num2;
break;
case '*':
output = num1 * num2;
break;
case '/':
output = num1 / num2;
break;
default:
System.out.printf("You have entered wrong operator");
return;
}
System.out.
html Program
<!DOCTYPE html>
<html>
<body>
<form>
<input type="text" id="display" disabled>
<br><br>
<input type="button" value="1" onclick="document.getElementById('display').value+='1'">
<input type="button" value="2" onclick="document.getElementById('display').value+='2'">
<input type="button" value="3" onclick="document.getElementById('display').value+='3'">
<input type="button" value="+" onclick="document.getElementById('display').value+='+'">
<br>
<input type="button" value="4" onclick="document.getElementById('display').value+='4'">
<input type="button" value="5" onclick="document.getElementById('display').value+='5'">
<input type="button" value="6" onclick="document.getElementById('display').value+='6'">
<input type="button" value="-" onclick="document.getElementById('display').value+='-'">
<br>
<input type="button" value="7" onclick="document.getElementById('display').value+='7'">
<input type="button" value="8" onclick="document.getElementById('display').value+='8'">
<input type="button" value="9" onclick="document.getElementById('display').value+='9'">
<input type="button" value="*" onclick="document.getElementById('display').value+='*'">
<br>
<input type="button" value="c" onclick="document.getElementById('display').value=''">
<input type="button" value="0" onclick="document.getElementById('display').value+='0'">
<input type="button" value="=" onclick="document.getElementById('display').value=eval(document.getElementById('display').value)">
<input type="button" value="/" onclick="document.getElementById('display').value+='/'">
<br>
</form>
</body>
</html>