191k views
0 votes
Write a program which accepts two integers from the user. Then it asks the user what he wants to do with those two numbers. If the user choice is

‘A’ – Add the two numbers

‘B’ – Subtract the second number from the first number

‘C’ – Multiply the first number by the second number

‘D’ – Divide the first number by the second number

‘Q’ – End the operation

Your program should continue to accept the two numbers and the user choice unless the user enters ‘Q’ as choice.

User Azimuts
by
5.3k points

1 Answer

4 votes

Answer:

// program in C++.

#include <bits/stdc++.h>

using namespace std;

// function to print choice menu

void menu()

{

cout<<"A – Add the two numbers:"<<endl;

cout<<"B – Subtract the second number from the first number:"<<endl;

cout<<"C – Multiply the first number by the second number:"<<endl;

cout<<"D – Divide the first number by the second number:"<<endl;

cout<<"Q – End the operation:"<<endl;

}

// driver function

int main()

{

// variables

int x,y;

char ch;

int flag=1;

do{

cout<<"Enter first number:";

// read first number

cin>>x;

cout<<"Enter second number:";

// read second number

cin>>y;

// call menu function

menu();

cout<<"Enter your choice:";

// read choice

cin>>ch;

switch(ch)

{

case 'A':

case 'a':

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

break;

case 'B':

case 'b':

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

break;

case 'C':

case 'c':

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

break;

case 'D':

case 'd':

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

break;

case 'Q':

case 'q':

flag=0;

}

if(flag==0)

break;

}while(ch!='Q'||ch!='q');

return 0;

}

Step-by-step explanation:

Read two numbers from user.Then read the choice for operation on both the numbers. Then print the choices of operation by calling menu() function.If choice is "A" then print Addition of both.if choice is "B" then print subtraction of second from first number.if choice is "C" then print the multiplication of both numbers.If choice is "D" then print division of first by second.If the choice is "Q" then quit the operation. Repeat this until user's choice is "Q".

Output:

Enter first number:6

Enter second number:2

A – Add the two numbers:

B – Subtract the second number from the first number:

C – Multiply the first number by the second number:

D – Divide the first number by the second number:

Q – End the operation:

Enter your choice:A

Addition of two numbers is:8

Enter first number:8

Enter second number:2

A – Add the two numbers:

B – Subtract the second number from the first number:

C – Multiply the first number by the second number:

D – Divide the first number by the second number:

Q – End the operation:

Enter your choice:Q

User ILearn
by
6.0k points