Write a calculator program using a switch statement that: a) Prompts the user to enter two numbers b) Prompts the user to select between the four arithmetic operations using a choice from 1-4; for example, prompt the user with: "Choose 1) for add, 2) for subtract, 3) for multiply or 4) for divide)" c) Calculates the result of performing the selected operation on the numbers, and d) Displays the answer on the screen (15 pts)

Respuesta :

Answer:

// calculator program in C++.

#include <bits/stdc++.h>

using namespace std;

// main function

int main()

{

// variables

int x,y;

int ch;

   cout<<"Enter 1st number:";

   // read first number

   cin>>x;

   cout<<"Enter 2nd number:";

   // read second number

   cin>>y;

   cout<<"Choose 1) for add, 2) for subtract, 3) for multiply or 4) for divide):";

   // read choice

   cin>>ch;

   switch(ch)

   {

       // if choice is 1, find Addition

       case 1:

       cout<<"Addition of two numbers is:"<<(x+y)<<endl;

       break;

       // if choice is 2, find subtraction

       case 2:

       cout<<"Subtract the second from the first is:"<<(x-y)<<endl;

       break;

       // if choice is 3, find multiplication

       case 3:

       cout<<"Multiplication of two numbers is:"<<(x*y)<<endl;

       break;

       // if choice is 4, find division

       case 4:

       cout<<"Division the first by the second is:"<<(x/y)<<endl;

       break;

       // if choice is other

       default:

       cout<<"Wrong choice:"<<endl;

       break;

   }

return 0;

}

Explanation:

Read two numbers from user.Then read the choice of operation from user.If choice is 1, then print the Addition of both numbers.If choice is 2, then print the subtraction. If the choice is 3, then print the multiplication.If choice is 4, then print the  division.If choice is other than these, then print wrong choice.

Output:

Enter 1st number:10                                                                                                      

Enter 2nd number:2                                                                                                      

Choose 1) for add, 2) for subtract, 3) for multiply or 4) for divide):3                                                    

Multiplication of two numbers is:20