Question

Write a program that mimics a calculator. The program should take as input two integer and...

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??

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program to take input of two integer variable a and b. After taking input...
Write a program to take input of two integer variable a and b. After taking input of these variables print all the palindrome numbers from a to b. I need this is C programming language.
Implement the Calculator code, this's C++ /* Implement a calculator take the input for a character...
Implement the Calculator code, this's C++ /* Implement a calculator take the input for a character if character is "+", then it's going to take the input of two numbers and show the sum if character is "-", then it's going to take the input of two numbers and show the minus result if character is "*", then it's going to take the input of two numbers and show the product if character is "/", then it's going to take...
1.Write pseudocode for a program that allows the user to input two numbers (X and Y)...
1.Write pseudocode for a program that allows the user to input two numbers (X and Y) and a code C. If the code has value 1, the program should output the larger of X and Y and otherwise output the smaller. Now convert your pseudocode to an assembly language program. Enter the program into the lab software assembler. Assemble the program and execute it with different input data. 2. Write pseudocode for a program that allows the user to input...
Write a python program that functions as a calculator. The program should run in an infinite...
Write a python program that functions as a calculator. The program should run in an infinite loop. It should ask the user for two numbers and then offer four choices: 1) Add 2) Subtract 3) Multiply 4) Divide Once it gets the operation from the user, it should call one of four functions to perform the operation and display the result. The functions should be placed in another module called calc.py
(must be in C program) Write a program that reads triplets from an input file, into...
(must be in C program) Write a program that reads triplets from an input file, into a, operator, and b and then computes. The program must warn users if division by zero or an unknown operator. The operator is one of the four standard operators +, -, /, *. a and b are integers. For sample input file: 1 + 5 5 / 0 3 * 2 It must generate output: 1 + 5 = 6 5 / 0 =...
Write a program in C++ that reads integer values from standard input and writes to standard...
Write a program in C++ that reads integer values from standard input and writes to standard output the smallest of the inputs and the average input value. The program should stop reading when a value equal to -1 or greater than 100 is encountered. It should also stop when an incorrect integer value (i.e., anything that isn't an int) is entered. If there is not an integer value in the input, the program should output "no integers provided"
Collapse Write a program that prompts the user to input a positive integer. It should then...
Collapse Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Turn in: Your source code for with The name of your program as a comment at the top...
 Write a program to find the prime numbers - Ask user to input the integer...
 Write a program to find the prime numbers - Ask user to input the integer number - test the number whether it is a prime number or not - Then, print “true” or “false” depending on whether the number is prime or isn’t. - Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. - Use idea of user input, cumulative sum,...
PHP calculator problem Create a calculator class that will add, subtract, multiply, and divide two numbers....
PHP calculator problem Create a calculator class that will add, subtract, multiply, and divide two numbers. It will have a method that will accept three arguments consisting of a string and two numbers example ("+", 4, 5) where the string is the operator and the numbers are what will be used in the calculation. It doesn't need an HTML form, all the arguments are put in through the method. The class must check for a correct operator (+,*,-,/), and a...
write a MIPS assembly program to get an integer input from the user and multiply it...
write a MIPS assembly program to get an integer input from the user and multiply it by 2 and print output to the console
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT