w3resource

C++ Exercises: Print the area of a polygon

C++ Basic: Exercise-58 with Solution

Write a C++ program 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?



Follow us on Facebook and Twitter for latest update.

C++ Programming: Tips of the Day

Why is there no std::stou?

The most pat answer would be that the C library has no corresponding "strtou", and the C++11 string functions are all just thinly veiled wrappers around the C library functions: The std::sto* functions mirror strto*, and the std::to_string functions use sprintf.

Ref: https://bit.ly/3wtz2qA

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook