Question

Q2- Write a C program that work as a calculator for the 4 main operations (...

Q2- Write a C program that work as a calculator for the 4 main operations ( + * / - ), an error message should be displayed when the user attempts to divide by zero.
The user input should be in this form:

Number operation number, ex: 2-3‏

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
 *  C Program for a two input arithmetic calculator
 */

#include <stdio.h>

int main()
{
  int x,y;
  char c;
  printf("Enter two numbers and operator: ");
  scanf("%d %c %d", &x, &c, &y);

  switch(c)
  {
    case '+'  :
                printf("Result (%d %c %d): ", x, c, y);
                printf("%d\n", x+y);
                break;

    case '-'  :
                printf("Result (%d %c %d): ", x, c, y);
                printf("%d", x-y);
                break;

    case '*'  :
                printf("Result (%d %c %d): ", x, c, y);
                printf("%d", x*y);
                break;
    
    case '/'  :
                if (y)
                {
                  printf("Result (%d %c %d): ", x, c, y);
                  printf("%f", (float)x/y);
                }
                else
                  printf("Error: Divide by zero");
                break;

    default   :
                printf("Invalid Operation");
  }

  return 0;
}

Add a comment
Know the answer?
Add Answer to:
Q2- Write a C program that work as a calculator for the 4 main operations (...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations must be...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for the list of operations that can be calculated and get the input from the user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • Write an assembler program that asks the user (as shown below) for two integers and a...

    Write an assembler program that asks the user (as shown below) for two integers and a single character representing the arithmetic operations: addition, subtraction, multiplication and integer division (displays both quotient and remainder). Perform the requested operation on the operands and display the result as specified below. Assume SIGNED NUMBERS. The program should not divide by 0! If the user attempts to divide by 0, display the error message: "Cannot divide by zero." If this error occurs, the program should...

  • CENGAGE MINDTAP Programming Exercise 4-8 Instructions Instructions Write a program that mimics a calculator. The program...

    CENGAGE MINDTAP Programming Exercise 4-8 Instructions Instructions Write a program that mimics a calculator. The program should take as input: 1. The first integer 2. The second integer 16 3. The operation to be 11 performed ( + . 12 13 ess 14 It should then output the 15 numbers, the operator, and the er 16 result. (For division, if the 17 c denominator is zero, output an 18 19 ( appropriate message. The 20 message should contain the word...

  • Question 20: Write a python program that will perform the operations of simple calculator. The operations...

    Question 20: Write a python program that will perform the operations of simple calculator. The operations are : Addition, subtraction, Multiplication and division. Sample output : Select operation. 1.Add 2.Subtract 3.Multiply 4.Divide Enter choice(1/2/3/4): 3 Enter first number: 15 Enter second number: 14 15 * 14 = 210

  • C Program: Trap divide by zero. Write a main (include libraries as always) that asks the...

    C Program: Trap divide by zero. Write a main (include libraries as always) that asks the user to enter two numbers(up to 100 times). For each pair of numbers divide the first number by the second and print the result. Set up and use the signal handler to trap the divide by zero, give the user a pleasant greeting before exiting. Test with no zeroes before forcing the error by entering zero for the second number

  • Write a program to create a set operation calculator applying object oriented programming principles discussed in...

    Write a program to create a set operation calculator applying object oriented programming principles discussed in the class so far. The set operation calculator need to be enclosed inside one class with different methods responsible for performing the different operations listed below. When the constructor (def __init__() ) is called during object instantiation, two separate objects of this class need to be instantiated two sets S1 and S2. These two set objects can be instantiated from two python lists taken...

  • **This is for the C Language. Trigonometric Calculator Specification You are required to write a program...

    **This is for the C Language. Trigonometric Calculator Specification You are required to write a program that calculates and displays values of the trigonometric functions sine, cosine, and tangent for user-supplied angular values. The basic program must comply with the following specification. 1. When the program is run, it is to display a short welcome message. TRIG: the trigonometric calculator 2. The program shall display a message prompting the user to enter input from the keyboard. Please input request (h-help,...

  • Write a C++ Program Write a program that prompts the user to input a number. The...

    Write a C++ Program Write a program that prompts the user to input a number. The program should then output the number and a message saying whether the number is positive, negative, or zero. You must insert the following comments at the beginning of your program and write our commands in the middle: Write a C++ Program: 1 Name: Your Name ID: Your ID #include <iostream> using namespace std; din main YOUR CODE HERE

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT