Java Program to Arithmetic Calculator using switch case Statements

Description

Get a binary arthmetic expression and solve the expression.
Input:
12 + 9
Output:
21
Input:
4 * 5
Output:
20

Solution

import java.util.*;
class ArCal
{
    public static void main(String args[])
    {
	int num1,num2;
        double output=0;
	char operator;
        Scanner sc=new Scanner(System.in);
	System.out.println("Enter a Binary Expression:");
	num1=sc.nextInt();
     	operator=sc.next().charAt(0);
      num2=sc.nextInt();
        switch(operator)
	{
		case '+':
			output=num1+num2;
			break;
		case '-':
			output=num1-num2;
			break;
		case '*':
			output=num1*num2;
			break;
		case '/':
			output=num1/num2;
			break;
		case '%':
			output=num1%num2;
			break;
		default :
			System.out.println("invalid operation");
			break;
	}
	System.out.println("The Output Is:"+output);
   }
}

We are Teaching "Web Development using Python" through Online.

View Tutor Profile and Course Syllabus

Explanation

Based on the  choice, instructions in any one of the case will be executed.

Break statement very important in switch.
Ramesh P Natarajan

Author - Ramesh P Natarajan

Software Developer and Technology Trainer, very much passionate in the field of Software Application Development, Machine Learning and Search Engine Optimization.

Write to: [email protected]





Interesting Pattern Programs: for more Patterns

Input:
5
Output:
*
**
***
****
*****
****
***
**
*Solution
Input:
5
Output:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *Solution
Input:
5
Output:
*        *
**      **
***    ***
****  ****
**********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********
**** ****
***   ***
**     **
*       *Solution
Input:
5
Output:
*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********Solution