
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Calculate Area of Circle
The area of a circle is a fundamental mathematical concept that can be easily calculated using a simple formula. In C++, there are multiple ways to find the area of a circle. In this article, we are going to discuss various approaches to calculating the area of a circle in C++.
How to Find the Area of a Circle?
The formula for finding the area of a circle with a given radius r is:
Area = ? à r à r
Example 1
- Input: r = 5
- Output: 78.54
Explanation:
The area of a circle with radius 5 is: ? Ã 5 Ã 5 = 78.54
Example 2
- Input: r = 3
-
Output: 28.27
Explanation:
The area of a circle with radius 3 is: ? Ã 3 Ã 3 = 28.27
Different Approaches to Find the Area of a Circle
Using Direct Formula Approach
This is the simple and direct approach for finding the area of a given circle. In this approach, we use the formula Area = ? à r à r.
Steps for Implementation
- First, take the input radius.
- Calculate the area using the formula: Area = ? à r à r.
- Output the result.
Implementation Code
#include<bits/stdc++.h> #define PI 3.14159 using namespace std; int main() { double r = 5; double area = PI * r * r; cout << "The area of the circle with radius " << r << " is: " << area << endl; return 0; }
Output
The area of the circle with radius 5 is 78.54
Time Complexity: O(1)
Space Complexity: O(1)
Using the M_PI Constant from the Library
In this approach, we use the predefined constant M_PI from the
Steps for Implementation
- Include the <cmath> library separately, or include the <bits/stdc++.h> library which includes all standard C++ libraries, including <cmath>.
- Use M_PI to calculate the area.
- Output the result.
Implementation Code
#include<iostream> #include<cmath> using namespace std; int main() { double r = 3; double area = M_PI * r * r; cout << "The area of the circle with radius " << r << " is: " << area << endl; return 0; }
Output
The area of the circle with radius 3 is 28.27
Time Complexity: O(1)
Space Complexity: O(1)
Using a Function
In this approach, we define a function that accepts the radius as an argument and returns the area of the circle. This approach is reusable and allows us to calculate the area of any circle by calling the function multiple times.
Steps for Implementation
- Define a function that accepts the radius and calculates the area.
- Call the function with the required input.
- Output the result.
Implementation Code
#include<iostream> #include<cmath> using namespace std; // Function to calculate the area of a circle double calculate_area(double r) { return M_PI * r * r; } int main() { double r = 3; double area = calculate_area(r); cout << "The area of the circle with radius " << r << " is: " << area << endl; return 0; }
Output
The area of the circle with radius 3 is 28.27
Time Complexity: O(1)
Space Complexity: O(1)
Using a Loop (Multiplication)
This is a simple and iterative approach. In this approach, we use a loop to multiply the radius by itself and ?. This is not the most efficient method, but it is used to understand the iterative process.
Steps for Implementation
- Initialize a variable to hold the result.
- Multiply the radius by itself inside a loop.
- Multiply the result by ? and output the result.
Implementation Code
#include<iostream> #define PI 3.14159 using namespace std; int main() { double r = 3; double result = 1; for (int i = 0; i < 2; i++) { result *= r; } result *= PI; cout << "The area of the circle with radius " << r << " is: " << result << endl; return 0; }
Output
The area of the circle with radius 3 is 28.27
Time Complexity: O(1)
Space Complexity: O(1)