
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 the Area of a Circle
What is a Circle?
A circle is a round two-dimensional shape in which all the points on the surface of the circle are equal from a point known as the center. The distance between the center and any point on the boundary of a circle is known as the radius.
Area of a Circle
The area of a circle is the total area present inside the boundary of a circle. In this article, we are going to discuss different approaches on how we can calculate the area of a circle using C#.
The formula for the Area of the Circle: ? à r²
Where ? (pi) is approximately 3.14 or can be represented using Math.PI in C#.
r is the radius of the given circle.
Example
Input:
radius = 5 cm
Output:
Area = 78.54 cm2
Input:
radius = 10 cm
Output:
Area = 314.159 cm2
Input:
radius = 0 cm
Output:
Area = 0 cm2
Calculating Circle Area Using Direct Formula Approach
The area of the circle is calculated using the direct formula.
The formula for calculating the area of a circle if the radius is given: Area of Circle = ? à radius², where the value of ? is 3.14. After calculating the area of circle using the formula, we return the area of the circle.
Steps for Implementation
- We take radius as input.
- Use the formula for
area = ? à radius²
. - Return the calculated area.
Implementation Code
using System; class Program { static void Main() { double radius = 5; // area of the circle using the formula double area = Math.PI * Math.Pow(radius, 2); // Display the result Console.WriteLine($"The area of the circle with radius {radius} is: {area}"); } }
Output:
The area of the circle with radius 5 is: 78.5398163397448
Time Complexity: O(1)
Space Complexity: O(1)
Calculating Circle Area Using Function
We can use a function to calculate the area of a circle. The logic is the same, only we define another function. We define a function to calculate the area of the circle. We pass the radius as a parameter to the function. Finally, we return the calculated area from the function.
Steps for Implementation
- We first define a function to calculate the area of a circle.
- Now, pass the radius as an argument to the function.
- Return the calculated area from the function.
Implementation Code
using System; class Program { static double CalculateArea(double radius) { return Math.PI * Math.Pow(radius, 2); } static void Main() { double radius = 6; // Call the function double area = CalculateArea(radius); // Display the result Console.WriteLine($"The area of the circle with radius {radius} is: {area}"); } }
Output:
The area of the circle with radius 6 is: 113.09733552923255
Time Complexity: O(1)
Space Complexity: O(1)