Question

Using C Q3. Prompt the user to enter two operands and one operation. Prompt for operation...

Using C

Q3. Prompt the user to enter two operands and one operation. Prompt for operation first -> ‘+’,’-’,’*’ or ‘/’, accept the arithmetic sign for the operation as user’s input and then accordingly specify the types of operands in prompt for operands. Then carry out the operation and print the result.

Show two sample outputs in screen captures - one for division and another for any other operation of your choice. Use switch construct.

Fill the below blanks -

Datatype used to store operation is _________.

For division without loss of remainder, at least one operand must be of type _____________.

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

Fill the below blanks -

  • Datatype used to store operation is ___char______.

For division without loss of remainder, at least one operand must be of type ____double_________.

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

Screenshot

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

Program

//Header file
#include<stdio.h>
//MAin function
int main(){
   //Variables declaration for user input
    char ch;
   int val1, val2;
   double val3;
   //Prompt for operator
   printf("Enter operator: ");
   scanf(" %c", &ch);
   //Error check
   while (ch != '+'&&ch != '-'&&ch != '*'&& ch != '/') {
       printf("Error!!!!Operator should be +-* or /.\n");
       printf("Enter operator: ");
       scanf(" %c", &ch);
   }
   //Carry out operations
   switch (ch) {
       //Addition
   case '+':
       printf("Enter two numbers: ");
       scanf("%d %d", &val1, &val2);
       printf("Sum of %d %c %d %c %d\n", val1, ch, val2, '=', (val1 + val2));
       break;
       //Subtraction
   case '-':
       printf("Enter two numbers: ");
       scanf("%d %d", &val1, &val2);
       printf("Difference of %d %c %d %c %d\n", val1,ch, val2, '=', (val1 - val2));
       break;
       //Multiplication
   case '*':
       printf("Enter two numbers: ");
       scanf("%d %d", &val1, &val2);
       printf("Product of %d %c %d %c %d\n", val1, ch, val2, '=', (val1 * val2));
       break;
       //Division
   case '/':
       printf("Enter two numbers: ");
       scanf("%lf %d",&val3,&val2);
       while (val2 == 0) {
           printf("Error!Division overflow\n");
           printf("Enter two numbers: ");
           scanf("%lf %d", &val3, &val2);
       }
       printf("Quotient of %.2lf %c %d %c %.2lf\n", val3, ch, val2, '=', (val3/val2));
       break;
   }
    return 0;
}

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

Output

output1:-

Enter operator: /
Enter two numbers: 4 0
Error!Division overflow
Enter two numbers: 4 3
Quotient of 4.00 / 3 = 1.33

output2:-

Enter operator: =
Error!!!!Operator should be +-* or /.
Enter operator: +
Enter two numbers: 15 16
Sum of 15 + 16 = 31

Add a comment
Know the answer?
Add Answer to:
Using C Q3. Prompt the user to enter two operands and one operation. Prompt for operation...
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 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...

  • Division by Zero Problem: Create a program titled Division. Have the program prompt the user "Enter...

    Division by Zero Problem: Create a program titled Division. Have the program prompt the user "Enter numerator and denominator" and store the two values entered. (If you wish the user to "Enter numerator/denominator" be sure to adjust your data gathering variables to include a holder variable for the slash character.) Include a function titled Quotient that takes 2 integers as input and provides the quotient of the 2 integers as integer output. (The remainder is truncated. Thus, 5/3 will be...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • Hi I need some help in C programing class and I doing one of the exercise...

    Hi I need some help in C programing class and I doing one of the exercise so that I can get better at coding. Suppose you are asked to design a software that helps an elementary school student learn multiplication and division of one-digit integer numbers. The software allows the student to select the arithmetic operation she or he wishes to study. The student chooses from a menu one of two arithmetic operations: 1) Multiplication and 2) Division (quotient). Based...

  • C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression...

    C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression convertor Stacks A stack is an abstract data type which uses a sequential container and limits access to that container to one end. You may enter or remove from the container, but only at one end. Using the Linked List data structure from your last homework assignment, implement a Stack of type string. The Stack should only have one data member: the Linked List....

  • PROBLEM STATEMENT The mini-calculator will use a small ALU to perform arithmetic operations on two 4-bit values which are set using switches. The ALU operations described below are implemented with a...

    PROBLEM STATEMENT The mini-calculator will use a small ALU to perform arithmetic operations on two 4-bit values which are set using switches. The ALU operations described below are implemented with an Adder/Subtractor component. A pushbutton input allows the current arithmetic result to be saved. An upgraded mini-calculator allows the saved value to be used in place of B as one of the operands. The small ALU that you will design will use the 4-bit adder myadder4 to do several possible...

  • this is a C++ program! You will write a program that performs the following tasks for...

    this is a C++ program! You will write a program that performs the following tasks for a simple mathematical calculator: (1) Open a user-specified file for input. Prompt for the name of the file, read it into a string variable, echo print it to the terminal and then open it. If the file is not opened, enter into a loop that prints out an error message, resets the input file stream variable (see the hints section), obtains a new file...

  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

  • Name: True/False & Multiple Choice (2 points each) (True / False ) 2 A char literal...

    Name: True/False & Multiple Choice (2 points each) (True / False ) 2 A char literal '2', a string literal "2", and the numeric literal 2 are identical and stored the same way though they are different types. 3 Since comments do not affect the program execution, you don't need to have it at all. 4. After the following statements are executed, the value of the variable rem is 3. 1. A preprocessor directive line starts with a pound sign...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

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