Question

c++ /Write a C++ program that creates a switch that adds and subtracts two numbers respectively.

c++ /Write a C++ program that creates a switch that adds and subtracts two numbers respectively.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

To do basic operation addition and subtraction i used switch case and shown the output in case of any problem let me know in comments.

Code:-

//including header files

#include<iostream>

using namespace std;

//main method

int main()

{

//taking input

float a,b;

//input first variable

cout<<"Enter Ist no: ";

cin>>a;

//input second variable

cout<<"Enter Second no: ";

cin>>b;

//Menu

cout<<"\nMenu\n1.Addition\n";

cout<<"2.Subtraction\n3.Exit\n";

//menu for calculation

bool loop=true;

//loop continues until valid input is given

while(loop)

{           

   cout<<"Enter your choice: ";

   //choice input

   int choice;

   cin>>choice;

   //switch case

   switch(choice)

   {

              case 1://addition

                             cout<<"Addition: "<<(a+b)<<endl;

                             loop=false;

                             break;

              case 2://subtraction

                             cout<<"Subtraction: "<<(a-b)<<endl;

                             loop=false;

                             break;

              case 3://Exit

                             return 0;

              default://run if other than above input is given

                             cout<<"wrong input Try again\n";

                             break;

              }

}

return 0;

}

Output:-

Add a comment
Know the answer?
Add Answer to:
c++ /Write a C++ program that creates a switch that adds and subtracts two numbers respectively.
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
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