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...
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 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
(8 marks) Write a program to ask user to input an integer and display the special...
Write a program to ask user to input an integer and display the special pattern accordingly. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use loop statements (for, while or do-while). Your program should use only the following 3 output statements, one of EACH of the followings: System.out.print("-"); // print # System.out.print("+"); // print + System.out.println(); // print a newline Your code must work exactly like the following example (the text in bold...
Write a MATLAB program to determine the factorial value of an input integer between 1 and...
Write a MATLAB program to determine the factorial value of an input integer between 1 and 30. You may NOT use the built-in factorial function. You must implement it using a for loop. Display the result back to the user with 2 decimal places (yes, 2 decimal places). The result should follow the following format:   The factorial value is *.xx
Write a full C++ program to read a series of integer numbers from standard input and...
Write a full C++ program to read a series of integer numbers from standard input and print them out in reverse order, one per line. You may assume that there will be a minimum of 2 numbers to read and a maximum of 200.
Write a program that takes in an integer in the range 20-98 as input. The output...
Write a program that takes in an integer in the range 20-98 as input. The output is a countdown starting from the integer, and stopping when both output digits are identical. I already did the coding part:num = int(input()) if 20 <= num <= 98: while num % 11!=0: print(num) num -= 1 print(num) else: print('Input must be 20-98') Question: How do you know when to use %? Why is it %11 and why not %12 or %15? How do...
Write a program in C++ coding that asks the user to input an integer between 25...
Write a program in C++ coding that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2...
Write a program that asks the user to input an integer between 25 and 50, inclusive....
Write a program that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2 relational expressions. You...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT