How to Find Area of a Circle with Diameter in C# | Functions
How to find area of a circle with diameter in C# using functions.
In this article, you will learn how to find area of a circle with diameter in C# using functions.
Example
Enter the radius of the circle::
15
Diameter = 30 units
Circumference = 94.2 units
Area = 706.5 sq. units
You should have knowledge of the following topics in C# programming to understand this program:
- C# Oops Concepts
- C# System Library
Source Code
// How to Find Area of a Circle with Diameter in C#
using System;
public class W3CW {
static public void Main (string[] args) {
Console.WriteLine("Enter the radius of the circle::\n");
float r=Convert.ToInt32(Console.ReadLine());
float d, c, a;
// It will calculate the diameter, circumference, and area
d = 2 * r;
c = 2 * (float)3.14 * r;
a = (float)3.14 * (r * r);
// It will print the final output
Console.WriteLine("Diameter = "+d+" units");
Console.WriteLine("Circumference = "+c+" units");
Console.WriteLine("Area = "+a+" sq. units");
}
}
Output
Enter the radius of the circle::
15
Diameter = 30 units
Circumference = 94.2 units
Area = 706.5 sq. units
Explanation
In the above program, we have taken input 12
from the user via the system console. Then we applied the standard formula to calculate the diameter, circumference, and area.
Standard formulas are given at the top of the article near the example portion can see there.
After the whole calculation, It will return these values following:
- Diameter = 30 units
- Circumference = 94.2 units
- Area = 706.5 sq. units
How to Find Area of a Circle with Diameter in C# using Math.PI Constant
// How to Find Area of a Circle with Diameter in C# using Math.PI Constant
using System;
public class W3CW {
static public void Main (string[] args) {
Console.WriteLine("Enter the radius of the circle::\n");
float r=Convert.ToInt32(Console.ReadLine());
double d, c, a;
// It will calculate the diameter, circumference, and area
d = 2 * r;
c = 2 * Math.PI * r;
a = Math.PI * (r * r);
// It will print the final output
Console.WriteLine("Diameter = "+d+" units");
Console.WriteLine("Circumference = "+c+" units");
Console.WriteLine("Area = "+a+" sq. units");
}
}
Output
Enter the radius of the circle::
15
Diameter = 30 units
Circumference = 94.2477796076938 units
Area = 706.858347057703 sq. units
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
using System;
public class W3CW {
// To calculate the diameter
public static float diameter(float r) {
return 2 * r;
}
// To calculate the circumference
public static float circumference(float r) {
return 2 * (float)3.14 * r;
}
// To calculate the area
public static float area(float r) {
return (float)3.14 * (r * r);
}
// It's the driver function
static public void Main (string[] args) {
Console.WriteLine("Enter the radius of the circle::\n");
float r=Convert.ToInt32(Console.ReadLine());
// It will print the final output
Console.WriteLine("Diameter = "+diameter(r)+" units");
Console.WriteLine("Circumference = "+circumference(r)+" units");
Console.WriteLine("Area = "+area(r)+" sq. units");
}
}