Computer Science, asked by libra2027, 6 months ago

Write a java program for arithmetic operation by taking input as arithmetic operator like '+', '-' , '*' , '/' , '%' using switch case

Answers

Answered by anindyaadhikari13
1

Question:-

Write a java program for arithmetic operation by taking input as arithmetic operator like '+', '-' , '*' , '/' , '%' using switch case.

Program:-

import java.util.*;

class Calculation

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.print("Enter the first number: ");

int a=sc.nextInt();

System.out.print("Enter the second number: ");

int b=sc.nextInt();

System.out.println("Enter your choice, +, -, *, /, %");

char ch=sc.next().charAt(0);

int result=0;

boolean x=true;

switch(ch)

{

case '+': result=a+b;

break;

case '-': result=a-b;

break;

case '*': result=a*b;

break;

case '/': result=a/b;

break;

case '%': result=a%b

break;

default: System.out.println("Invalid Choice.");

x=false;

}

if(x)

System.out.println("Result is: "+result);

}

}

Similar questions