Advertisement
Shekhar777

Java Loops and Introduction to Arrays - Basic Calculator

Oct 19th, 2020
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. Q12-Calculators are widely used devices nowadays. It makes calculations easier and faster. A simple calculator consists of the following operators:
  2. 1. '+': adding two numbers
  3. 2. '- ': subtraction
  4. 3. '*': multiplying numbers
  5. 4. '/': division
  6. You will be given operator '+' or '- ' or '*' or '/' followed by two operands(integers), you need to perform a mathematical operation based on a given operator.
  7. If '/' operator is used, return the integer value after division
  8. Ans-static int calculator(char ch, int a, int b){
  9.     int result=0;
  10.          // your code here
  11.          switch(ch) {
  12.              case '+':{
  13.                  result = a+b;
  14.                  break;
  15.             }
  16.              case '-':{
  17.                  result = a-b;
  18.                  break;
  19.             }
  20.              case '*':{
  21.                  result = a*b;
  22.                  break;
  23.             }
  24.              case '/':{
  25.                  result = a/b;
  26.                  break;
  27.             }
  28.             default :{
  29.                      break;  
  30.             }        
  31.  
  32.  
  33.              }
  34. return(result);
  35.          }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement