How to Find Area of a Circle with Diameter in Java | Functions
How to find area of a circle with diameter in Java using functions.
In this article, you will learn how to find area of a circle with diameter in Java using functions.
Example
Enter the radius of the circle::
7
Diameter = 14.0 units
Circumference = 43.960003 units
Area = 153.86 sq. units
Standard Formula
D = 2 * r
C = 2 * PI * r
A = PI * r2
r = radius of the circle
D = diameter of the circle
C = circumference of the circle
A = area of the circle
You should have knowledge of the following topics in Java programming to understand this program:
- Java
java.util.Scanner
package - Java
main()
method - Java
System.out.println()
function
1. By using the default value of PI = 3.14
// How to Find Area of a Circle with Diameter in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the radius of the circle::\n");
float r = in.nextFloat();
float d;
float c;
float a;
// r = radius, d = diameter, c = circumference, a = area
// It will calculate the diameter, circumference, and area
d = 2 * r;
c = 2 * (float)3.14 * r;
a = (float)3.14 * (r * r);
System.out.println("\nDiameter = " + d + " units");
System.out.println("Circumference = " + c + " units");
System.out.println("Area = " + a + " sq. units");
}
}
Output
Enter the radius of the circle::
7
Diameter = 14.0 units
Circumference = 43.960003 units
Area = 153.86 sq. units
Explanation
In the above program, we have taken input 7
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 = 14.0 units
- Circumference = 43.960003 units
- Area = 153.86 sq. units
2. By using Math.PI constant
// How to Find Area of a Circle with Diameter in Java using Math.PI
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the radius of the circle::\n");
float r = in.nextFloat();
float d;
float c;
float a;
// r = radius, d = diameter, c = circumference, a = area
// It will calculate the diameter, circumference, and area
d = 2 * r;
c = (float)(2 * Math.PI * r);
a = (float)(Math.PI * Math.pow(r, 2));
System.out.println("\nDiameter = " + d + " units");
System.out.println("Circumference = " + c + " units");
System.out.println("Area = " + a + " sq. units");
}
}
Output
Enter the radius of the circle::
7
Diameter = 14.0 units
Circumference = 43.982296 units
Area = 153.93803 sq. units
How to Find Area of a Circle with Diameter in Java using Functions
// How to Find Area of a Circle with Diameter in Java using Functions
import java.util.Scanner;
public class Main {
// 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
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the radius of the circle::\n");
float r = in.nextFloat(); // r = radius
// It will print the final output
System.out.println("\nDiameter = " + Diameter(r) + " units");
System.out.println("Circumference = " + Circumference(r) + " units");
System.out.println("Area = " + Area(r) + " sq. units");
}
}