133k views
1 vote
Develop a menu-driven program that inputs two numbers and, at the user’s option, finds their sum, difference, product, or quotient.

User Md Rahman
by
4.5k points

1 Answer

3 votes

Answer:

import java.util.Scanner;

public class TestClock {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter num 1");

double num1 = in.nextDouble();

System.out.println("Enter num 2");

double num2 = in.nextDouble();

System.out.println("enter \"s\" for addition\t \"d\" for difference\t \"p\" for the product\t and \"q\" for the quotient");

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

if(op =='s'||op=='S'){

double sum = num1+num2;

System.out.println("The sum is "+sum);

}

else if(op =='d'||op=='D'){

double diff = num1-num2;

System.out.println("The difference is "+diff);

}

else if(op =='p'||op=='P'){

double prod = num1*num2;

System.out.println("The product is "+prod);

}

else if(op =='q'||op=='Q'){

double quo = num1/num2;

System.out.println("The Quotient is "+quo);

}

}

}

Step-by-step explanation:

  1. Use Scanner class to receive the two numbers and save in a two variables
  2. Create a new variable for Operation (op) to store a character
  3. Request the user to enter a character for the opearation (e.g s=sum, p=product, q=quotient and d=difference)
  4. Use if/else..if statements to implement the required operation

User Piotr Dobrogost
by
4.4k points