0

How do I make a calculator

6th Nov 2022, 3:59 AM
Noah Calderon
Noah Calderon - avatar
4 Answers
6th Nov 2022, 4:26 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 1
import java.util.Scanner; public class Calculator { public static void main(String[] args) { double num1; double num2; double ans; char op; Scanner reader = new Scanner(System.in); System.out.print("Enter two numbers: "); num1 = reader.nextDouble(); num2 = reader.nextDouble(); System.out.print(" Enter an operator (+, -, *, /): "); op = reader.next().charAt(0); switch(op) { case '+': ans = num1 + num2; break; case '-': ans = num1 - num2; break; case '*': ans = num1 * num2; break; case '/': ans = num1 / num2; break; default: System.out.printf("Error! Enter correct operator"); return; } System.out.print(" The result is given as follows: "); System.out.printf(num1 + " " + op + " " + num2 + " = " + ans); } }
7th Nov 2022, 1:07 PM
Noah Calderon
Noah Calderon - avatar
+ 1
How do u do it in python
8th Nov 2022, 3:18 AM
Noah Calderon
Noah Calderon - avatar