Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Q12-Calculators are widely used devices nowadays. It makes calculations easier and faster. A simple calculator consists of the following operators:
- 1. '+': adding two numbers
- 2. '- ': subtraction
- 3. '*': multiplying numbers
- 4. '/': division
- 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.
- If '/' operator is used, return the integer value after division
- Ans-static int calculator(char ch, int a, int b){
- int result=0;
- // your code here
- switch(ch) {
- case '+':{
- result = a+b;
- break;
- }
- case '-':{
- result = a-b;
- break;
- }
- case '*':{
- result = a*b;
- break;
- }
- case '/':{
- result = a/b;
- break;
- }
- default :{
- break;
- }
- }
- return(result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement