Expt 2
Expt 2
2
i. Aim:
To write a menu driven C++ program to calculate area of rectangle, triangle and circle
using switch-case statement.
ii. Algorithm:
Step 1: START
Step2: Declare variables l, b, ba, and h, r of type int to store values of length,
breadth, base, height and radius.
Step 3: Read values of all above variables from user.
Step 4: Display a menu to calculate area of rectangle, triangle or circle.
Step 5: Declare a variable choice and read it’s value from user.
Step 6: Use switch-case statement to display area of figure of given choice.
Step 7: Pass the value of choice to switch statement.
Step 8: STOP.
iii. Source code:
#include<iostream>
using namespace std;
int main()
{ int l,b,ba,h,r,choice;
//read values of variables from user
cout<< ” \n Enter value of length and breadth of rectangle: “;
cin>>l>>b;
cout<< ” \n Enter value of base and height: “;
cin>>b>>h;
cout<< ” \n Enter value of radius: “;
cin>>r;
// Menu to select a particular option
Cout<< “\n Press 1 for area of rectangle”;
Cout<< “\n Press 2 for area of triangle”;
Cout<< “ \n Press 3 for area of circle”;
Cin>> choice;
Switch (choice)
{
Case 1: cout<< “\n Area of rectangle is: “ << l*b ;
break;
Case 2: cout<< “\n Area of triangle is: “ << 0.5*h*ba ;
break;
Case 3: cout<< “\n Area of circle is: “ << 3.14*r*r ;
break;
default: cout<<” \n Wrong choice”;
}
return 0;
}
iv. Sample Inputs and outputs:
Enter value of length and breadth of rectangle: 8 4
Enter value of base and height: 6 4
Enter value of radius: 7
Press 1 for area of rectangle
Press 2 for area of triangle
Press 3 for area of circle
1
Area of rectangle is: 32
v.Result:
The program is successfully compiled & executed, and the output is verified.
vi. Questions related to the Experiment:
1. How do structures in C and C++ differ?
2. What is a class? How does it accomplish data hiding?
3. Write down 4 advantages of object oriented programming.