# Printing Result of Two Integers Based on Operator Entered Using Switch-Case in Java

In Java programming, the switch-case statement provides a convenient way to perform different operations based on the value of an operator. In this tutorial, we will explore how to print the result of two integers based on the operator entered using switch-case. This step-by-step guide is tailored for new learners to understand the concept, syntax, and examples of switch-case statements in action.

## Understanding the Problem

The problem at hand involves taking two integers and an operator as input and then performing a specific operation based on the operator entered. The switch-case statement enables us to simplify this decision-making process by executing the appropriate code block according to the operator.

## Implementing the Program: Step-by-Step

Let’s break down the implementation of the program into step-by-step instructions:

• Step 1: Take two integer inputs: `num1` and `num2`.
• Step 2: Take an operator input as a character.
• Step 3: Use the switch-case statement on the operator variable.
• Step 4: Define cases for different operators and perform the corresponding operation in each case.
• Step 5: Add a default case to handle invalid operators.
• Step 6: Print the result obtained from the switch-case block.

## Example: Printing Result of Two Integers

Consider an example where we take two integers `num1 = 10`, `num2 = 5`, and the operator is ‘+’:

int num1 = 10;
int num2 = 5;
char operator = ‘+’;
int result;

switch (operator) {
case ‘+’:
result = num1 + num2;
break;
case ‘-‘:
result = num1 – num2;
break;
case ‘*’:
result = num1 * num2;
break;
case ‘/’:
result = num1 / num2;
break;
default:
System.out.println(“Invalid operator”);
return;
}

System.out.println(“Result: ” + result);

The output for this example will be: “Result: 15”.

## Conclusion

By utilizing the switch-case statement in Java, we can efficiently handle different operators and perform corresponding operations on two integers. The program we discussed simplifies decision-making and enhances code readability. Understanding the syntax and examples provided will enable you to implement similar logic in your Java programs effectively.

Keep practicing and exploring the possibilities of switch-case statements in Java coding.

Happy programming!