Java program to calculate the area of a circle
In this post, you will learn how to calculate the area of a circle using Java program.
As we all know, the area of the circle is the measure of the space or region enclosed inside the circle. In geometry, the area enclosed by a circle of radius r is πr2. Here is the simple formula for calculating the area of the circle -
Area = pi * r2
Java program to calculate the area of circle using static method
Here is the Java program to calculate and display the area of a circle. This program asked the user to enter the radius of the circle as input. We have stored this input in a variable and calculated the area of the circle using a formula.
import java.util.Scanner;
public class AreaOfCircle
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Please enter the radius:");
double radius = s.nextDouble();
double area = (22*radius*radius)/7 ;
System.out.println("Area of circle is: " + area);
}
}
Output of the above code:
Please enter the radius: 15
Area of circle is: 707.143
Java program to calculate the area of circle using user-defined method
Here is the program to calculate the area of the circle using the user-defined method.
import java.util.Scanner;
public class AreaOfCircle
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Please enter the radius : ");
double r = s.nextDouble();
double a = circleArea(r);
System.out.println("Area of Circle : " + a);
}
static double circleArea(double r)
{
return (22*r*r)/7;
}
}
Output of the above code:
Please enter the radius : 22
Area of Circle : 1521.1428
Java program to calculate area of circle using diameter
In the given example, we have calculated the area of circle using diameter. Diameter is the distance across the circle passing through the center. We can also calculate the area of the circle if we know the diameter.
import java.util.Scanner;
public class AreaOfCircle
{
public static void main(String args[])
{
double diameter, area;
System.out.println("Enter the diameter of the circle: ");
Scanner sc = new Scanner(System.in);
diameter = sc.nextDouble();
area=Math.PI*(diameter*diameter)/4;
System.out.println("The area of the circle is: "+area);
}
}
Output of the above code:
Enter the diameter of the circle:
The area of the circle is: 201.0619
Java program to calculate area of circle using circumference
The circle's circumference is the distance around the circle. We can easily calculate the area of a circle if we know the circumference using a formula. A= C²⁄ 4π, where C is the circumference. The following Java program demonstrates this -
import java.util.Scanner;
public class AreaOfCircle
{
public static void main(String args[])
{
double circumference, area;
Scanner sc=new Scanner (System.in);
System.out.println("Enter the circumference of the circle: ");
circumference=sc.nextDouble();
area=(circumference*circumference)/(4*Math.PI);
System.out.println("The area of the circle is: "+area);
}
}
Output of the above code:
Enter the circumference of the circle: 19
The area of the circle is: 28.72746722808711
Related Articles
Automorphic number in JavaPascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java