C is the language Write a program that performs simple addit

C++ is the language

Write a program that performs simple addition, subtraction, multiplication, and division. It should first ask the user to choose which operation they wish to perform, then prompts the user for two numbers. The program should pass the two numbers to one of the following functions, depending on which operation the user selects: addition(), subtraction(), multiplication(), division(). Make sure not to allow zero as the second input when performing division.

Example:

Choose an Operation:

1. Addition

2. Subtraction

3. Multiplication

4. Division

Your choice? 3

Enter the first number? 4

Enter the second number? 5

4 multiplied by 5 is 20

Solution

#include <iostream>
using namespace std;
int addition(int a,int b)
{
   return a+b;
}
int subtraction(int a, int b)
{
   return a-b;
}
int multiplication(int a, int b)
{
   return a*b;
}
float division(int a, int b)
{
   return (a*1.0)/b;
}
int main()
{
   cout<<\"Choose an Operation:\"<<endl<<\"1. Addition\"<<endl<<\"2. Subtraction\"<<endl<<\"3. Multiplication\"<<endl<<\"4. Division\"<<endl<<\"Your choice?\";
   int op;
   int a,b;
   cin>>op;
   if(op>4 || op<1)
   {
       cout<<\"Cannot understand operation\"<<endl;
       return 0;
   }
   cout<<\"Enter the first number?\";
   cin>>a;
cout<<\"Enter the second number?\";
   cin>>b;
   int c;
   float d;
   if(op==4 && b == 0)
   {
       cout<<\"Cannot divide by zero.\"<<endl;
       return 0;
   }

   if(op==1)
   {
       c=addition(a,b);
       cout<<a<<\" added to \"<<b<<\" is \"<<c<<endl;
   }
   else if(op==2)
   {
       c=subtraction(a,b);
       cout<<b<<\" subtracted from \"<<a<<\" is \"<<c<<endl;
   }
   else if(op==3)
   {
       c=multiplication(a,b);
       cout<<a<<\" multiplied to \"<<b<<\" is \"<<c<<endl;
   }
   else if(op==4)
   {
       d=division(a,b);
       cout<<a<<\" divided by \"<<b<<\" is \"<<d<<endl;
   }
   return 0;
}

C++ is the language Write a program that performs simple addition, subtraction, multiplication, and division. It should first ask the user to choose which opera
C++ is the language Write a program that performs simple addition, subtraction, multiplication, and division. It should first ask the user to choose which opera

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site