English, asked by kaiswarya9797, 8 days ago

Java program , should ask you to enter two numbers and operator. Depending upon that operator you will perform addition,subtraction, multiplication and division

Answers

Answered by ItzMeSam35
3

import java.util.Scanner;

public class Calculator

{

public static void main(String args[])

{

double num1 , num2 , result = 0 ;

Scanner scan = new Scanner(System.in);

System.our.println(" Calculator Menu");

System.out.println(" ===============");

System.out.println(" (A)ddition");

System.out.println(" (S)ubtraction");

System.out.println(" (M)ultiplication");

System.out.println(" (D)ivision");

System.out.println(" (R)emainder");

System.out.println(" (E)xit");

System.out.println();

char choice = scan.next().charAt(0);

switch(choice)

{

case 'A':

num1 = AcceptInput(1);

num2 = AcceptInput(2);

result = num1 + num2;

break;

case 'S':

num1 = AcceptInput(1);

num2 = AcceptInput(2);

result = num1 - num2;

break;

case 'M':

num1 = AcceptInput(1);

num2 = AcceptInput(2);

result = num1 * num2;

break;

case 'D':

num1 = AcceptInput(1);

num2 = AcceptInput(2);

result = num1 / num2;

break;

case 'R':

num1 = AcceptInput(1);

num2 = AcceptInput(2);

result = num1 % num2;

break;

case 'E':

System.out.println("Thanks For Using The Calculator");

System.exit(0);

default:

System.out.println("Invalid Option Entered");

System.exit(0);

}

System.out.println("Result is : "+ result);

scan.close();

}

public static double AcceptInput (int i)

{

Scanner scan=new Scanner(System.in);

System.out.println("Please Enter Number " + i + " : ");

double num = scan.nextDouble();

return num ;

}

}

Similar questions