Write Calculator program in java


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.




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>
















No comments:

Post a Comment