Geometry Functions
Implement a program that provides 4 functions:
2 for calculating the circumference of a circle, and a rectangle. 2 for calculating the area of a circle, and rectangle.
The circles data consists of its radius. The rectangles data consists of it's length and width.
Include
<cmath>in your program to get access to π by using the constantM_PI.
Use the fucntions to print statements regarding a circle and a rectangle.
Expected output
Use a circle with radius of 5 and rectangle with length 4 and height 6 to test.
Circle with radius 5:
Circumference: 31.4159
Area: 78.5398
Rectangle with length 4 and width 6:
Perimeter: 20
Area: 24
Answer
#include <iostream>
#include <cmath> // For M_PI (π)
// Circle calculations
double circle_circumference(double radius)
{
return 2 * M_PI * radius;
}
double circle_area(double radius)
{
return M_PI * radius * radius;
}
// Rectangle calculations
double rectangle_perimeter(double length, double width)
{
return 2 * (length + width);
}
double rectangle_area(double length, double width)
{
return length * width;
}
int main()
{
// Test the functions (for demonstration purposes)
double radius = 5.0;
std::cout << "Circle with radius " << radius << ":\n";
std::cout << "Circumference: " << circle_circumference(radius) << "\n";
std::cout << "Area: " << circle_area(radius) << "\n\n";
double length = 4.0, width = 6.0;
std::cout << "Rectangle with length " << length << " and width " << width << ":\n";
std::cout << "Perimeter: " << rectangle_perimeter(length, width) << "\n";
std::cout << "Area: " << rectangle_area(length, width) << "\n";
}