Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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:

C++ Exercises: Print the area of a polygon

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:

Flowchart: Print the area of a polygon

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?