How to Find Area of a Circle with Diameter in C++ using Functions | Class | Function Overloading | Constructor
How to find area of a circle with diameter in C++ using functions, class, function overloading, and constructor.
In this article, you will learn how to find area of a circle with diameter in C++ using functions, class, function overloading, and constructor.
Example
Enter the radius of the circle::
7
Diameter :: = 14 units
Circumference :: = 43.96 units
Area :: = 153.86 sq. units
You should have knowledge of the following topics in c++ programming to understand this program:
- C++
cin
object - C++
cout
object - C++ Functions
- C++ Oops
- C++ Function overloading
Formula
D = 2 * r
C = 2 * PI * r
A = PI * r2
Where
R = radius of the circle
D = diameter of the circle
C = circumference of the circle
A = area of the circle
There are two ways to solve these problems:
- By using the default value of PI = 3.14
- Second, by using the M_PI constant
In this article, we solve this problem in six methods:
- Using the normal calculation
- Using the functions
- Using the Class and Object
- Using the function overloading
- Using the Inline Function
- Using the Constructor
1. Using the default value of PI = 3.14
// How to Find Area of a Circle with Diameter in C++
#include <iostream>
using namespace std;
int main() {
float r, d = 0, c = 0, a = 0;
// r = radius of the circle
// d = diameter of the circle
// c = circumference of the circle
// a = the area of the circle
cout << "Enter the radius of the circle::\n";
cin >> r;
// It will calculate the diameter, circumference, and area of the circle
d = 2 * r;
c = 2 * 3.14 * r;
a = 3.14 * (r * r);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
return 0;
}
Output
Enter the radius of the circle::
7
Diameter :: = 14 units
Circumference :: = 43.96 units
Area :: = 153.86 sq. units
Explanation
In this given program, we have taken the input 7
the radius of the circle from the user and then applied the standard formula to find the Diameter, Circumference, and Area of the circle.
Then it will return the output values of this program as follows:
- Diameter is
14
units - Circumference is
43.96
units - Area is
153.86
sq. units
2. Using the M_PI constant
// How to Find Area of a Circle with Diameter in C++ using the M_PI constant
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float r, d = 0, c = 0, a = 0;
// r is the radius of the circle
// d is the diameter of the circle
// c is the circumference of the circle
// a is the area of the circle
cout << "Enter the radius of the circle::\n";
cin >> r;
// It will calculate the diameter, circumference, and area of the circle
d = 2 * r;
c = 2 * M_PI * r;
a = M_PI * (r * r);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
return 0;
}
How to Find Area of a Circle with Diameter in C++ using Functions
// How to Find Area of a Circle with Diameter in C++ using Functions
#include <iostream>
using namespace std;
// This function will calculate the area of the circle
void CalculateArea(float x) {
float d = 0, c = 0, a = 0;
// d is the diameter of the circle
// c is the circumference of the circle
// a is the area of the circle
// It will calculate the diameter, circumference, and area of the circle
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
// It's the driver function
int main() {
float r; // radius of the circle
cout << "Enter the radius of the circle::\n";
cin >> r;
CalculateArea(r);
return 0;
}
Enter the radius of the circle::
16
Diameter :: = 32 units
Circumference :: = 100.48 units
Area :: = 803.84 sq. units
How to Find Area of a Circle with Diameter in C++ using Class and Object
// How to Find Area of a Circle with Diameter in C++ using Class and Object
#include <bits/stdc++.h>
using namespace std;
class Calculation {
public:
// This method will calculate the area of the circle
void CalculateArea(float x) {
float d = 0, c = 0, a = 0;
// d is the diameter of the circle
// c is the circumference of the circle
// a is the area of the circle
// It will calculate the diameter, circumference, and area of the circle
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
};
// It's the driver function
int main() {
float r; // radius of the circle
cout << "Enter the radius of the circle::\n";
cin >> r;
// To make the instances of the `Calculation` class
Calculation CA;
CA.CalculateArea(r);
return 0;
}
Output
Enter the radius of the circle::
12
Diameter :: = 24 units
Circumference :: = 75.36 units
Area :: = 452.16 sq. units
How to Find Area of a Circle with Diameter in C++ using Function Overloading
// How to Find Area of a Circle with Diameter in C++ using Function Overloading
#include <bits/stdc++.h>
using namespace std;
// This function will calculate the area of the circle
// function with int data type parameter
void CalculateArea(int x) {
float d = 0, c = 0, a = 0;
// It will calculate the diameter, circumference, and area of the circle
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
// function with float data type parameter
void CalculateArea(float x) {
float d = 0, c = 0, a = 0;
// d is the diameter of the circle
// c is the circumference of the circle
// a is the area of the circle
// It will calculate the diameter, circumference, and area of the circle
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
// It's the driver function
int main() {
int a = 7;
float b = 10.4;
// call function with int data type parameter
CalculateArea(a);
// call function with float data type parameter
CalculateArea(b);
return 0;
}
Output
Diameter :: = 14 units
Circumference :: = 43.96 units
Area :: = 153.86 sq. units
Diameter :: = 20.8 units
Circumference :: = 65.312 units
Area :: = 339.622 sq. units
How to Find Area of a Circle with Diameter in C++ using inline Function
// How to Find Area of a Circle with Diameter in C++ using inline Function
#include <bits/stdc++.h>
using namespace std;
// This function will calculate the area of the circle
inline void CalculateArea(float x) {
float d = 0, c = 0, a = 0;
// d is the diameter of the circle
// c is the circumference of the circle
// a is the area of the circle
// It will calculate the diameter, circumference, and area of the circle
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
// It's the driver function
int main() {
float r; // radius of the circle
cout << "Enter the radius of the circle::\n";
cin >> r;
CalculateArea(r);
return 0;
}
How to Find Area of a Circle with Diameter in C++ using Constructor
// How to Find Area of a Circle with Diameter in C++ using Constructor
#include <bits/stdc++.h>
using namespace std;
class Area {
public:
float d = 0, c = 0, a = 0;
// d is the diameter of the circle
// c is the circumference of the circle
// a is the area of the circle
// It will calculate the diameter, circumference, and area of the circle
Area(float x) {
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
}
// It will produce the final output
void Output() {
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
};
// It's the driver function
int main() {
float r; // radius of the circle
cout << "Enter the radius of the circle::\n";
cin >> r;
// It will create the instance of the `Area` class
Area AR(r);
AR.Output();
return 0;
}
Output
Enter the radius of the circle::
20
Diameter :: = 40 units
Circumference :: = 125.6 units
Area :: = 1256 sq. units
Also, visit these links
C Program to Enter Two Numbers and Perform All Arithmetic Operations