Monday, 24 September 2018

Menu driven program for arithmetic operation in C++

Menu driven program

A menu driven program is a program in which the menu is displayed and the operation is performed according to it. A menu with all the possible operation is displayed. The user's choice according to the menu is taken and the corresponding operation is performed.


Write a program in C++ to input two numbers from the user. Calculate the addition, subtraction, multiplication or division of two numbers according to the choice of the user.


You can perform the following operations:
1. Addition
2. Subtraction
3. Multiplication 
4. Division
5. Exit

We need to input two numbers and display the above menu. Then we will input the operation the user wants to perform. Using the switch case we will perform the arithmetic operation for respective choices and the result will be displayed.



Program



#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c,choice;
clrscr();
cout<<"Enter 1st number=";
cin>>a;
cout<<"Enter 2nd number=";
cin>>b;
cout<<"\nYou can perform the following operations:\n";
cout<<"1.Addition\n";
cout<<"2.Subtraction\n";
cout<<"3.Multiplication\n";
cout<<"4.Division\n";
cout<<"5.Exit\n";
cout<<"\nEnter your choice=";
cin>>choice;
switch(choice)
{
case 1:
c=a+b;
cout<<"\nAddition="<<c;
break;
case 2:
c=a-b;
cout<<"\nSubtraction="<<c;
break;
case 3:
c=a*b;
cout<<"\nMultiplication="<<c;
break;
case 4:
c=a/b;
cout<<"\nDivision="<<c;
break;
case 5:
cout<<"\nThank you";
break;
default:
   cout<<"\nInvalid choice";
}
getch();
}

Output



Program to create a menu driven calculator
MENU DRIVEN PROGRAM FOR ARITHMETIC OPERATION














Explanation


We entered two numbers 5 and 3 (i.e. a=3 and b=5). Then we entered 3 as our choice, so the multiplication of two input numbers (i.e. a*b = 3*5) was performed. Then the result (i.e. c=15) was displayed on the screen.






No comments:

Post a Comment