Answer to Question #250086 in Java | JSP | JSF for Amadix

Question #250086

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.





1
Expert's answer
2021-10-12T01:45:12-0400
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the operation to perform: ");
        System.out.println("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division");
		int option = in.nextInt();
		System.out.println("Enter the two numbers to perform the operation: ");
		System.out.print("Operand 1: ");
		int X = in.nextInt();
		System.out.print("Operand 2: ");
		int Y = in.nextInt();
		double total;
		switch (option) {
		case 1:
		{
			total = X + Y;
			System.out.println("Addition of the two numbers is = " + total);
		}
			break;
		case 2:
		{
			total = X - Y;
			System.out.println("Subtraction of the two numbers is = " + total);
		}
			break;
		case 3:
		{
			total = X * Y;
			System.out.println("Multiplication of the two numbers is = " + total);
		}
			break;
		case 4:
		{
			if (Y != 0) {
				total = X / Y;
				System.out.println("Division of the two numbers is= " + total);
			} else {
				System.out.println("Division by zero not allowed");
			}
		}
			break;




		}
		in.close();
	}
}

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

Ama - Toby
13.10.21, 02:50

Thanks so much, Perfectly answered

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS