Computer Science, asked by chinni2458, 8 months ago

Lucy has entered to Narnia through the wardrobe. Once she enters to Narnia, she is encountered with a genie. Genie takes care of Narnia from trespassers. As Genie finds Lucy to be genuine, Genie allows Lucy to enter Narnia if she answers his question. He gives Lucy two numbers and asks her to perform the basic arithmetic operation like addition, subtraction etc. She need to perform the same and tell the answer. If she tells right answer, she gets the pass to Narnia. The input should consists of two integers and the operation to be performed which should be corresponding integer. Print the Menu as

Answers

Answered by lalpankaj0
17

Answer:

#include <iostream>

using namespace std;

int main(){

int c,a,b;

cout<<"Enter first number : ";

cin>>a;

cout<<"Enter second number : ";

cin>>b;

cin>>c;

cout<<"Menu"<<"\n";

cout<<"1.Addition"<<"\n";

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

cout<<"3.Multiplication"<<"\n";

cout<<"4.Division"<<"\n";

cout<<"5.Remainder"<<"\n";

switch(c){

case 1:

cout<<a+b;

break;

case 2:

cout<<a-b;

break;

case 3:

cout<<a*b;

break;

case 4:

cout<<a/b;

break;

case 5:

cout<<a%b;

break;

default:

cout<<"Invalid operation";

}

return 0;

}

Answered by ankhidassarma9
0

Answer:

# include <iostream>

using namespace std;

int main()

{

 char op;

 float num1, num2;

 cout << "Enter operator: +, -, *, /: ";

 cin >> op;

 cout << "Enter two operands: ";

 cin >> num1 >> num2;

 switch(op)

{

   case '+':

     cout << num1 << " + " << num2 << " = " << num1 + num2;

     break;

   case '-':

     cout << num1 << " - " << num2 << " = " << num1 - num2;

     break;

   case '*':

     cout << num1 << " * " << num2 << " = " << num1 * num2;

     break;

   case '/':

     cout << num1 << " / " << num2 << " = " << num1 / num2;

     break;

   default:

     // If the operator is other than +, -, * or /, error message is shown

     cout << "Error! operator is not correct";

     break;

 }

 return 0;

}

Explanation :

  • The above program takes an operator and two operands as input from the user.
  • The operator is stored in variable named op and two operands are stored in num1 and num2 .
  • The switch-case statement is used to check the operator entered by user. If it is '+' then, statements for case: '+' will execute and program will be terminated.
  • If user enters '-' then, statements for case: '-' will be executed and program will terminate.
  • Similar way ,the program will work  for the * and / operators.
  • If the operator will not match any of the four character [ +, -, * and / ], then the default statement will be executed which will display error message.
Similar questions