No products in the cart.

Java coding for simple calculator

CBSE, JEE, NEET, CUET

CBSE, JEE, NEET, CUET

Question Bank, Mock Tests, Exam Papers

NCERT Solutions, Sample Papers, Notes, Videos

Java coding for simple calculator
  • 1 answers

Pranesh Raam 4 years, 5 months ago

import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter two numbers: "); // nextDouble() reads the next double from the keyboard double first = reader.nextDouble(); double second = reader.nextDouble(); System.out.print("Enter an operator (+, -, *, /): "); char operator = reader.next().charAt(0); double result; switch(operator) { case '+': result = first + second; break; case '-': result = first - second; break; case '*': result = first * second; break; case '/': result = first / second; break; // operator doesn't match any case constant (+, -, *, /) default: System.out.printf("Error! operator is not correct"); return; } System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result); } }
http://mycbseguide.com/examin8/

Related Questions

Founder of python
  • 2 answers

myCBSEguide App

myCBSEguide

Trusted by 1 Crore+ Students

Test Generator

Test Generator

Create papers online. It's FREE.

CUET Mock Tests

CUET Mock Tests

75,000+ questions to practice only on myCBSEguide app

Download myCBSEguide App