In this example, you will learn about C++ program to find area of the circle with and without using the function.
Formula to find area of the circle:
Area_circle = Π * r * r
where,
mathematical value of Π is 3.14159.
Let’s calculate the are of the circle using two methods.
//C++ program to calculate area of the circle
#include <iostream>
using namespace std;
int main() {
float radius, area_circle;
// take radius as input
cout << "Enter the radius of circle: ";
cin >> radius;
area_circle = 3.14 * radius * radius;
cout << "Area of circle: " << area_circle << endl;
return 0;
}
Output
Enter the radius of circle: 5 Area of circle: 78.5
//C++ program to find area of the circle using function
#include <iostream>
using namespace std;
//function declaration
float areaOfCircle(float radius_circle);
int main() {
float radius;
// take radius as input
cout << "Enter the radius of circle: ";
cin >> radius;
cout << "Area of circle: " << areaOfCircle(radius) << endl;
return 0;
}
// function definition to calculate area of circle
float areaOfCircle(float radius_circle)
{
float area_circle;
area_circle = 3.14 * radius_circle * radius_circle;
return area_circle; //return area
}
Output
Explanation
In the first program, we have simply calculated area of the circle in the main program.
However, we have used areaOfCircle( )
function for calculating area of the circle in the second program.