Question

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)

  1. Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation.
  2. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double.
  3. Calculations must be implemented using functions which return a double value.
  4. Result of the calculation should be displayed on the scree

Important notes: It has to be in C++ program

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

#include<iostream>

using namespace std;

double addition(double x, double y)

{

    return x + y;

}

double subtraction(double x, double y)

{

    return x - y;

}

double multiplication(double x, double y)

{

    return x * y;

}

double division(double x, double y)

{

    // division by 0 is not defined

    if( y == 0.0 )

        return 0.0;

   

    return x / y;

}

int main()

{

    double x, y, ans;

   

    // infinite loop

    while(1)

    {

        cout<<"Enter 2 numbers : ";

       

        // get user input

        cin>>x>>y;

       

        cout<<"\nSelect an option ...\n";

        cout<<"1. Addition\n";

        cout<<"2. Subtraction\n";

        cout<<"3. Multiplication\n";

        cout<<"4. Division\n";

        cout<<"5. exit\n";

       

        int ch;

       

        // get user input

        cin>>ch;

       

        // addition

        if( ch == 1 )

        {

            double ans = addition(x, y);

           

            cout<<x<<" + "<<y<<" : "<<ans<<endl;

        }

        // subtraction

        else if( ch == 2 )

        {

            double ans = subtraction(x, y);

           

            cout<<x<<" - "<<y<<" : "<<ans<<endl;

        }

        // multiplication

        else if( ch == 3 )

        {

            double ans = multiplication(x, y);

           

            cout<<x<<" * "<<y<<" : "<<ans<<endl;

        }

        // division

        else if( ch == 4 )

        {

            double ans = division(x, y);

           

            cout<<x<<" / "<<y<<" : "<<ans<<endl;

        }

        else

            break;

       

        cout<<endl;

    }

   

    return 0;

}


Sample Output

Add a comment
Know the answer?
Add Answer to:
Write a C++ Program that simulates a basic calculator using functions which performs the operations of...
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 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 a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • Problem 3 (35) Design a calculator that performs four operations: addition, multiplication, division and subtraction with...

    Problem 3 (35) Design a calculator that performs four operations: addition, multiplication, division and subtraction with 2 integer type numbers a) Ask user what type of operation to perform (+, , * or/) a. If the user inputs 'none' then the program terminates. Otherwise it will keep continuing the calculation. (hint: use while) b) Ask user to provide 2 integer number inputs c) Perform operation (whatever operation they mentioned in a) d) Print result e) In division operation, perform a...

  • in python and use while loop Exercise: Complete the program that performs basic arithmetic operations of...

    in python and use while loop Exercise: Complete the program that performs basic arithmetic operations of two input numbers. The program displays a menu for user to select a desired operation. ===== MAIN MENU ===== 1. Addition 2. Subtraction 3. Exit Select an operation (1-3): 1 Enter two numbers: 12 20 12 + 20 - 32 - MAIN MENU 1. Addition 2. Subtraction 3. Exit Select an operation (1-3): 2 Enter two numbers: 12 20 12 - 10 = 2...

  • PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which...

    PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which will simulate a calculator. Your calculator needs to support the five basic operations (addition, subtraction, multiplication, division and modulus) plus primality testing (natural number is prime if it has no non-trivial divisors). : Rewrite your lab 3 calculator program using functions. Each mathematical operation in the menu should be represented by a separate function. In addition to all operations your calculator had to support...

  • This is a quick little assignment I have to do, but I missed alot of class...

    This is a quick little assignment I have to do, but I missed alot of class due to the Hurricane and no one here knows what theyre doing. please help! this is Java with intelli-J Overview In this project students will build a four-function one-run calculator on the command line. The program will first prompt the user for two numbers, then display a menu with five operations. It will allow the user to select an option by reading input using...

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch...

    Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch statement and functions. 2. Write a program to find the summation of N numbers. Use two functions. One function will take the input from user and the other will perform the summation from 1 to N. 3. Write a program to find the factorial of a number. Use two functions. One function will take the input from user and the other will perform the...

  • 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

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