Write a program that mimics a calculator. The program should take as input two integer and an arithmetic operation (+, -, *, /, %) to be performed. It should then output the numbers the operator and the
result. Division by zero??
import java.io.IOException;
import java.util.Scanner;
public class Operation {
public static void main(String[] args) throws IOException
{
int numerator, denominator;
char operation;
Scanner in = new Scanner(System.in);
System.out.print("Enter your calculation: ");
numerator = in.nextInt();
denominator = in.nextInt();
operation = in.next().charAt(0);;
switch (operation) {
case 'x':
case '*':
System.out.println(numerator +" "+ operation +" "+ denominator +" result is " + (numerator * denominator));
break;
case '+':
System.out.println(numerator +" "+ operation +" "+ denominator +" result is " + (numerator + denominator));
break;
case '-':
System.out.println(numerator +" "+ operation +" "+ denominator +" result is " + (numerator - denominator));
break;
case '/':
case '%':
if (numerator == 0 || denominator == 0)
System.out.println("Cannot divide by zero");
else
System.out.println(numerator +" "+ operation +" "+ denominator +" result is " + (numerator / denominator));
break;
default:
System.out.println( "There was an error with your input");
break;
}
}
}
output:
Enter your calculations: 5 2 +
5 + 2 result is 7
Get Answers For Free
Most questions answered within 1 hours.