In: Computer Science
Define a problem with user input, user output, Switch and some
mathematical computation. Write the pseudocode, code and display
output.
Include source code and output. If no output explain the reason why
and what you are going to do make sure it does not happen again aka
learning from your mistakes.
Problem:
Pseudocode:
Code:
Output:
Problem : Find the Addition , Subtraction , Multiplication and Division using the Switch statement
Pseudocode :
Step 1: int select (for select the operation)
Step 2: Operation type:
1. Enter 1 for Addition
2. Enter 2 for subtraction
3. Enter 3 for multiplication
4. Enter 4 for Division
Step 3: Double x1,x2 ,result
Step 4: Enter the first value
Step 5: Enter the second value
Step 6: Use Switch statement.
Step 7: Switch(select)
{
case 1:
result= x1+x2;
print(result);
case 2:
result = x1-x2;
print(result);
case 3:
result=x1*x2;
print(result);
case 4:
result=x1/x2;
print(result);
default:
print("Select valid option");
}
Code:
import java.util.Scanner;
class Test {
public static void main(String[] args) {
int select;
Double x1, x2, result;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter value for performing the operation");
System.out.println("Enter 1 for Addition (+)");
System.out.println("Enter 2 for Subtraction(-)");
System.out.println("Enter 3 for Multiplication (*)");
System.out.println("Enter 4 for Division (/)");
select = scanner.nextInt();
System.out.println("Enter first value");
x1 = scanner.nextDouble();
System.out.println("Enter Second Value");
x2 = scanner.nextDouble();
switch (select) {
case 1:
result = x1 + x2;
System.out.print(x1 + "+" + x2 + " = " + result);
break;
case 2:
result = x1 - x2;
System.out.print(x1 + "-" + x2 + " = " + result);
break;
case 3:
result = x1 * x2;
System.out.print(x1 + "*" + x2 + " = " + result);
break;
case '/':
result = x1 / x2;
System.out.print(x1 + "/" + x2 + " = " + result);
break;
default:
System.out.println("Please select valid option");
break;
}
}
}
Output:
Thank you.................