Â
In this we are going to see how to make a Program in C++ to calculate Area of Different Shapes.
The code given below can be used for TURBO C++ Compiler:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, c;
float d;
cout << "Select the shape " << endl;
cout << " 1. Rectangle \n 2. Square \n 3. Triangle \n 4. Circle" << endl;
cin >> a;
switch (a)
{
case 1:
cout << "enter length and breadth of rectangle" << endl;
cin >> b >> c;
d = b * c;
cout << "Area of Rectangle is :" << d << endl;
break;
case 2:
cout << "enter side of the square" << endl;
cin >> b;
d = b * b;
cout << "Area of Square is :" << d << endl;
break;
case 3:
cout << "Enter base and height of triangle" << endl;
cin >> b >> c;
d = (0.5) * b * c;
cout << "Area of triangle is :" << d << endl;
break;
case 4:
cout << "enter radius of circle" << endl;
cin >> b;
d = 3.14 * b * b;
cout << "Area of Circle is :" << d << endl;
break;
default:
cout << "Please select a genuine option";
}
getch();
}
The code given below can be used for g++/gcc Compiler:-
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
float d;
cout << "Select the shape " << endl;
cout << " 1. Rectangle \n 2. Square \n 3. Triangle \n 4. Circle" << endl;
cin >> a;
switch (a)
{
case 1:
cout << "enter length and breadth of rectangle" << endl;
cin >> b >> c;
d = b * c;
cout << "Area of Rectangle is :" << d << endl;
break;
case 2:
cout << "enter side of the square" << endl;
cin >> b;
d = b * b;
cout << "Area of Square is :" << d << endl;
break;
case 3:
cout << "Enter base and height of triangle" << endl;
cin >> b >> c;
d = (0.5) * b * c;
cout << "Area of triangle is :" << d << endl;
break;
case 4:
cout << "enter radius of circle" << endl;
cin >> b;
d = 3.14 * b * b;
cout << "Area of Circle is :" << d << endl;
break;
default:
cout << "Please select a genuine option";
}
return 0;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP