Assignment
20
Write a C++ program to find area of triangle,
circle, and rectangle using function overloading.
Use formula to calculate area
as below:
Area of Triangle
= ( b * h) / 2
Area of Circle
= 3.14 * r * r
Area of Rectangle
= l * w
Program Code for run:
#include
<iostream>
using
namespace std;
void
area(float b, float h, float a)
{
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
cout << ” \t \t Area of
Triangle \n”;
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
cout << ” \t Value b =
” << b << ” \t Value h = ” << h ;
cout <<“\n \t \t Volume =
” << b * h * a << endl;
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
}
void
area(float r)
{
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
cout << ” \t \t Area of
Circle \n”;
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
cout << ” \t \t Value r =
” << r ;
cout <<“\n \t \t Volume =
” << 3.14 * r * r << endl;
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
}
void
area(float l, float w)
{
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
cout << ” \t \t Area of
Rectangle \n”;
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
cout << ” \t Value l =
” << l <<“\t w = ” << w ;
cout << ” \n \t \t Volume
= ” << l * w << endl;
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
}
int
main()
{
int n, l1, w1, r, h, b;
while(1)
{
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
cout << ” \t Calculate
the Area of Cube, Cylinder and Rectangle ” << endl;
cout << ” \t Triangle : 1
\t\t Circle : 2 \n”;
cout << ” \t Rectangle :
3 \t\t Exit : Other \n”;
cout << ” \t\t Choose
Appropriate option \n”;
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
cout << “\t”;
cin >> n;
cout << ” * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * ” << endl;
switch(n)
{
case 1: cout <<
” Enter the base of Triangle ” << endl << ” \t
“;
cin >> b;
cout << ” Enter the height of
Triangle ” << endl << ” \t “;
cin >> h;
area(b, h, 0.5);
break;
case 2: cout <<
” Enter the radius of Circle ” << endl << ” \t
“;
cin
>> r;
area(r);
break;
case 3: cout <<
” Enter the length of Rectangle ” << endl << ” \t
“;
cin
>> l1;
cout
<< ” Enter the width of Rectangle” << endl <<
” \t “;
cin
>> w1;
area(l1, w1);
break;
default: cout <<
” Thank You to Use this Program !”;
exit(0);
}
}
}
Output
of Program:
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Calculate the Area of Cube, Cylinder and
Rectangle
Triangle : 1 Circle : 2
Rectangle : 3 Exit : Other
Choose Appropriate option
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
1
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Enter the base of Triangle
5
Enter the height of Triangle
8
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Area of Triangle
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Value b = 5 Value h = 8
Volume = 20
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Calculate the Area of Cube, Cylinder and
Rectangle
Triangle : 1 Circle : 2
Rectangle : 3 Exit : Other
Choose Appropriate option
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
2
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Enter the radius of Circle
3
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Area of Circle
* * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * *
Value r = 3
Volume = 28.26
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Calculate the Area of Cube, Cylinder and
Rectangle
Triangle : 1 Circle : 2
Rectangle : 3 Exit : Other
Choose Appropriate option
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
3
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Enter the length of Rectangle
5
Enter the width of Rectangle
4
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Area of Rectangle
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Value l = 5
w = 4
Volume = 20
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Calculate the Area of Cube, Cylinder and
Rectangle
Triangle : 1 Circle : 2
Rectangle : 3 Exit : Other
Choose Appropriate option
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
5
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Thank You to Use this Program !