In this core java programming tutorial we will write a Calculator program which performs Addition, subtraction, multiplication and division of two numbers in java using switch statement.
Must read: How to find number is odd or even without % operator in java - tricky interview programs, How to Change case of characters in given string in java.
Write a Calculator program which performs Addition, subtraction, multiplication and division of two numbers in java using switch statement.
Full Program/SourceCode / Example to > Write a Calculator program which performs Addition, subtraction, multiplication and division of two numbers in java using switch statement >
import java.util.Scanner;
/*
* Write a Calculator program which performs Addition, subtraction, multiplication and division of two numbers in java.
*/
/** JavaMadeSoEasy.com */
public class CalculatorProgramUsingSwitchStatementExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number 1 : ");
int num1 = scanner.nextInt();
System.out.print("Enter number 2 : ");
int num2 = scanner.nextInt();
System.out.print("Enter > + for Addition, - for subtract, * for multiplication and / for division : ");
String operation = scanner.next();
/*
* Perform calculator operations
*/
switch (operation) {
case "+":
System.out.println("Add result > " + num1 + " + "+ num2 +" = " + (num1+num2));
break;
case "-":
System.out.println("Subtract result > " + num1 + " - "+ num2 +" = " + (num1-num2));
break;
case "*":
System.out.println("Multiply result > " + num1 + " * "+ num2 +" = " + (num1*num2));
break;
case "/":
System.out.println("Division result > " + num1 + " / "+ num2 +" = " + (num1/num2));
break;
default : //optional
System.out.println("Invalid Grade");
break; //optional
}
}
}
/*OUTPUT 1
Enter number 1 : 4
Enter number 2 : 2
Enter > + for Addition, - for subtract, * for multiplication and / for division : +
Add result > 4 + 2 = 6
*/
/*OUTPUT 2
Enter number 1 : 4
Enter number 2 : 2
Enter > + for Addition, - for subtract, * for multiplication and / for division : -
Subtract result > 4 - 2 = 2
*/
/*OUTPUT 3
Enter number 1 : 4
Enter number 2 : 2
Enter > + for Addition, - for subtract, * for multiplication and / for division : *
Multiply result > 4 * 2 = 8
*/
/*OUTPUT 4
Enter number 1 : 4
Enter number 2 : 2
Enter > + for Addition, - for subtract, * for multiplication and / for division : /
Division result > 4 / 2 = 2
*/
|
So, in this core java programming tutorial we wrote a Calculator program which performs Addition, subtraction, multiplication and division of two numbers in java using switch statement.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
>7) Write a Program on How to find number is binary or not program in java - basic interview programs
>10) Write a Program to calculate Occurrence of digit in cube of number in java - interview programs
>14) Write a Program to Find only duplicate number in given list in java - important interview programs