Answer to Question #250365 in Java | JSP | JSF for guru

Question #250365

Using a switch statement, write a program that prompts the user for a numeric code and two operands. The numeric codes of 1, 2, 3, and 4 represents addition, subtraction, multiplication, and division respectively. Before division, your program should check whether the second operand is not zero. If it is, it should display “Division by zero not allowed” to the screen.

Sample Run1

Code: 1

Operand1: 5

Operand2: 4

Output1: Sum = 9

Sample Run 2

Code: 3

Operand1: 5

Operand2: 4

Output2: Product = 20

Sample Run 3

Code: 4

Operand1: 5

Operand2: 0

Output3: Division by zero not allowed



1
Expert's answer
2021-10-14T07:55:13-0400
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Code: ");
        int code = in.nextInt();
        System.out.print("Operand1: ");
        int operand1 = in.nextInt();
        System.out.print("Operand2: ");
        int operand2 = in.nextInt();
        if (operand2 == 0) {
            System.out.println("Division by zero not allowed");
        } else {
            switch (code) {
                case 1:
                    System.out.println("Sum = " + (operand1 + operand2));
                    break;
                case 2:
                    System.out.println("Difference = " + (operand1 - operand2));
                    break;
                case 3:
                    System.out.println("Product = " + (operand1 * operand2));
                    break;
                case 4:
                    System.out.println("Quotient = " + (operand1 / operand2));
                    break;
            }
        }

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS