Question

(50 pts) Write a program that asks user an arithmetic operator('+','−','∗' or '/') and two operands...

(50 pts) Write a program that asks user an arithmetic operator('+','−','∗' or '/') and two operands and perform the corresponding calculation on the operands. Specific requirements: (1) Your main function accepts inputs and display calculation result.

(2) Write four functions to perform the four calculations. Call these functions in your main function.

(3) Repeatedly asks for inputs, calculate and display result until user input nonnumeric value for operands.

Hint: you can put getchar() right after scanf() to get rid of the newline character that comes though input to scanf(), so it won’t affect the following single character input either by scanf() or getchar().

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

#include<stdio.h>//header file
//function declaration for calculation
float add(float num1,float num2);
float sub(float num1,float num2);
float mul(float num1,float num2);
float divide(float num1,float num2);
//main function
float main()
{
   char operator;//for opeartor
    float first, second,result;//for operand
    while(1)
    {
    printf("Enter two operands: ");
    /*this condition take input and check whether the data is
    non-numeric or not, scanf always return true or false according to
    input so by using it we check the condition if nonnumeric found
    program will stop*/
    if(scanf(" %f %f", &first, &second) == 0){
    printf("exiting.........\n");
    return 0;
    }
    else
    {  
       printf("Enter an operator (+, -, *,/): ");
        scanf(" %c", &operator);//taking operator from user
       //switch statements for selcting calculation
       switch(operator)
       {
       case '+':
       result = add(first, second);
       break;
       case '-':
       result = sub(first, second);
       break;
       case '*':
       result = mul(first, second);
       break;
      
       case '/':
       result = divide(first, second);
       break;
       }
    printf("%f\n",result);
    }
    }

}
//for addition
float add(float num1,float num2)
{
   float result;
  
   result = num1 + num2;
   return result;
}
//for subtraction
float sub(float num1,float num2)
{
   float result;
  
   result = num1-num2;
   return result;
}
//for multiplication
float mul(float num1,float num2)
{
   float result;  
   result = num1*num2;
   return result;
}
//for divide
float divide(float num1,float num2)
{
   float result;
  
   result = num1/num2;
   return result;
}

Output and code

Add a comment
Know the answer?
Add Answer to:
(50 pts) Write a program that asks user an arithmetic operator('+','−','∗' or '/') and two operands...
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...

  • 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...

  • ​Write a C++ function int calculate(char, int , int ) which takes a char of either ‘+’, ‘ ‘--’, ‘*’, or ‘/’ as operator and two integers as operands.

    Write a C++ function int calculate(char, int , int ) which takes a char of either ‘+’, ‘ ‘--’, ‘*’, or ‘/’ as operator and two integers as operands. The function should return the result of applying the corresponding operator on the operands if all of them are of proper types and values. It should throw a char exception if the operator is unknown and a char* exception if the operator is ‘/’ and the second operand is zero. Write...

  • You are to write a program IN C++ that asks the user to enter an item's...

    You are to write a program IN C++ that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: if the an item's wholesale cost is 5.00 and its markup percentage is 100%, then the item's retail price is 10.00 If an item's wholesale cost is 5.00 and its markup percentage is 50%, then the item's retail price is 7.50 Program design specs. You should have 5...

  • Write a C Program that asks the user to input a string and then the user...

    Write a C Program that asks the user to input a string and then the user is prompted back whether the input string is a “Palindrome Word” or not. (A Palindrome Word reads the same backward or forward, eg. madam, racecar, etc.) Your program should contain a function that accepts the input string from the main function and returns a value indicating whether the input string is a Palindrome or not. Use Pointers to traverse the string.

  • 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...

  • 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...

  • MIPS programming question Problem 1: Write a program that asks the user to input a string...

    MIPS programming question Problem 1: Write a program that asks the user to input a string (or no more than 50 characters). Your program should then output the length of the string. The string length should be determined using a separate function strlen that will accept the address of the string and return its length. For the purposes of this exercise, the length of a string will be defined as the number of non-null and non-newline characters until either the...

  • Please help me write down a program to check email address valid or not. Tip:parse the...

    Please help me write down a program to check email address valid or not. Tip:parse the string and count the special characters like '@', '.' .and at least one character. Suggested libc functions: printf() 1.Have to be by the argc/argv of main function 2.Connot use scanf/getchar and other user input functions 3.Please explain the details and steps for me cause I'm a beginner hhhhhh Thank you soooo much for helping me!

  • 21 Write a program that asks the user to input the length and breadth of a...

    21 Write a program that asks the user to input the length and breadth of a soccer field, and then computes and returns the number of square meters of grass required to cover the field The formula for the area is the product of length and breadth (5) 22 The following program uses the break statement to terminate an infinite while loop to print 5 numbers Rewrite the program to use a while loop to display numbers from 1 to...

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