0% found this document useful (0 votes)
9 views

program of switch case

This Java program performs basic arithmetic operations (addition, subtraction, multiplication, division) on two user-input numbers based on a selected operator. It utilizes a switch-case structure to determine the operation based on the operator input. If the operator is not recognized, it outputs an error message.

Uploaded by

ribhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

program of switch case

This Java program performs basic arithmetic operations (addition, subtraction, multiplication, division) on two user-input numbers based on a selected operator. It utilizes a switch-case structure to determine the operation based on the operator input. If the operator is not recognized, it outputs an error message.

Uploaded by

ribhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Ques: Write a switch case program in Java to perform the following operations

between two numbers. a. Addition


b. Subtraction
c. Multiplication
d. Division
e. Exit

import java.util.Scanner;

public class Main


{

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");
}

System.out.println(first + " " + operator + " " + second + " = " + result);
}
}

Output

Enter two numbers: 1.5


4.5
Enter an operator (+, -, *, /): *
1.5 * 4.5 = 6.8

You might also like