menu driven calculator

MENU DRIVEN CALCULATOR
EX. NO :
DATE
:
SIMPLE MENU DRIVEN CALCULATOR PROGRAM USING SWITCH STATEMENT
AIM:
To write a simple menu driven program using switch statement.
ALGORITHM:
Step-1 start
Step-2 display menu
Step-3 read the two variable a and b
Step-4 enter the option code
Step-5 evaluate option code with case statements
Step-5.1 case =1 c=a+b
print c. goto step-7
Step 5.2 case=2
c=a-b
print c. goto step-7
Step 5.3 case=3
c=a*b
print c. goto step-7
Step 5.4 case=4
c=a/b
print c. goto step-7
Step 5.5 case=5
c=0 exit
Step-6 entered case option is invalid code the print “invalid operation code”
Step-7 stop
PSEUDOCODE:
1.
2.
3.
4.
5.
Begin the program by displaying the menu
Read the two variable a and b
Enter the option code
Evaluate option code with case constants.
Terminate the program.
MENU DRIVEN CALCULATOR
FLOW CHART
C
start
Case 1
read
C=a+b
If
Print c
A
N<=4 & N>0
Case 2
N
C=a-b
Read a,b
Print c
Switch n
B
Case 3
C=a*b
C
Print c
Case 4
C=a/b
Print c
Case 0
A
B
Print invalid operation code
MENU DRIVEN CALCULATOR
PROGRAM
#include <stdio.h>
#include<conio.h>
Void main()
{
Int a,b,c,n;
Clrscr();
Printf(“ \n –menu-- “);
Printf(“\n 1 addition”);
Printf(“\n 2 subtraction”);
Printf(“\n 3 multiplication”);
Printf(“\n4 division”);
Printf(“\n enter your choice : “);
Scanf(“%d”,&n);
If(n<=4 & n>0)
{
Printf(“\n enter two numbers”);
Scanf(“%d %d”,&a,&b);
}
switch(n)
{
case 1: c=a+b; printf(“\n Addition : %d”,c); break;
case 2: c=a-b; printf(“\n subtraction: %d”,c); break;
case 3: c=a*b; printf(“\n multiplication: %d”,c); break;
case 4: c=a/b; printf(“\n division:%d”,c); break;
default: printf(“\n invalid operation code”);
}
MENU DRIVEN CALCULATOR
getch();
}
OUTPUT :
--MENU-1 addition
2 subtraction
3 multiplication
4 division
Enter your choice : 1
Enter two numbers : 8 6
addition: 14
RESULT :
The program is executed and the output is verified.