C++ Exercises: Print the area of a polygon
C++ Basic: Exercise-58 with Solution
Write a program in C++ to print the area of a polygon.
Pictorial Presentation:
Sample Solution:
C++ Code :
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float ar,s,n;
cout << "\n\n Print the area of a polygon:\n";
cout << "---------------------------------\n";
cout << " Input the number of sides of the polygon: ";
cin>> n;
cout << " Input the length of each side of the polygon: ";
cin>> s;
ar= (n * (s * s)) / (4.0 * tan((M_PI / n)));
cout<<" The area of the ploygon is: "<<ar<<"\n";
}
Sample Output:
Print the area of a polygon: --------------------------------- Input the number of sides of the polygon: 7 Input the length of each side of the polygon: 6 The area of the ploygon is: 130.821
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C++ to print the area of a hexagon.
Next: Write a program in C++ to compute the distance between two points on the surface of earth.
What is the difficulty level of this exercise?