Question

In C program Write a program that mimics a calculator. The program should prompt for two...

In C program

Write a program that mimics a calculator.

The program should prompt for two integers and the operation to be performed.

It should then output the numbers, the operator, and the result. All the results should also be integers. For division, if the denominator is zero, output an appropriate message.

Example (Numbers and symbols with underscore indicate an input):

Enter integer +|-|*|/ integer: 3 + 2
3 + 2 = 5

--------------------------------------------

Enter integer +|-|*|/ integer: 13 * 5
13 * 5 = 65

--------------------------------------------

Enter integer +|-|*|/ integer: 5 / 0
5 / 0 = ERROR 
Cannot divide by zero

HINT: This might be a good place to use a switch statement!

Homework Answers

Answer #1
#include <stdio.h>

int main() {
    int n1, n2, result;
    char ch;
    printf("Enter integer +|-|*|/ integer: ");
    scanf("%d %c %d", &n1, &ch, &n2);
    printf("\n");
    switch (ch) {
        case '+':
            result = n1 + n2;
            break;
        case '-':
            result = n1 - n2;
            break;
        case '*':
            result = n1 * n2;
            break;
        case '/':
            if (n2 == 0) {
                printf("%d / 0 = ERROR\n"
                       "Cannot divide by zero\n", n1);
                return 1;
            }
            result = n1 / n2;
            break;
        default:
            printf("Error. Invalid operator\n");
            return 1;
    }
    printf("%d %c %d = %d\n", n1, ch, n2, result);
    return 0;
}
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 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??
Create a simple addition calculator in Java. The program should prompt the user to enter 2...
Create a simple addition calculator in Java. The program should prompt the user to enter 2 integers, then adds the numbers and prints the result. Make sure the program includes appropriate exception handling in case the user does not enter appropriate integer values.
write a C program that declares an integer variable called "favorite_number". The program should then prompt...
write a C program that declares an integer variable called "favorite_number". The program should then prompt the user to enter their favorite number, and use scanf to read the user's input into favorite_number. Finally, the program should print a message that includes the user's input.
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...
In C++ write a program for a multiplication table It must Prompt the user for two...
In C++ write a program for a multiplication table It must Prompt the user for two integers between 1 and 20 (inclusive) • also must Calculates and displays the multiplication table in a well-formatted output The table must include a label for each row and column The program must follow the requirements: • Validates user input, displaying an error message and prompting to user to enter another integer if the input is invalid, and repeating it as many times as...
2.20 TEST 2: the worst calculator ever Input Prompt the user for 2 integers. Enter an...
2.20 TEST 2: the worst calculator ever Input Prompt the user for 2 integers. Enter an integer: 5 Enter another integer: 2 Processing Modify the first integer by adding 1 to it and save the new value into a variable called modified_int1. Modify the second integer by subtracting 1 from it and save the new value into a variable called modified_int2. Compute the sum, difference, product, quotient, and exponentiation of modified_int1 and modified_int2 and save each into a variable. Print...
1) Create an interface to the keyboard (Scanner). Scanner is described in Chapter 2. 2) Prompt...
1) Create an interface to the keyboard (Scanner). Scanner is described in Chapter 2. 2) Prompt the user to enter their first and last name.. 3) Read the name into a String. 4) Print a person greeting such as “Hello FirstName LastName”. Print the users name in the greeting. 5) Prompt the user to enter 2 integers. 6) Read the integers into 2 variables into your program. 7) Prompt the user to enter a math operation – one of +,-,*,/...
(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 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
(Write in C++) Write a program that reads in two numbers and, if the input is...
(Write in C++) Write a program that reads in two numbers and, if the input is valid, outputs 2 times the product of the integers that lie between the two values (including the values themselves). If either number is not an integer, or if the first number is not less than the second number, just output an error message. The sample runs below should give the idea. User inputs are in bold. Important Notes: Your program should use a loop...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT