public class CircleAreaCircumference {
public static void main(String[] args) {
double radius = 5;
double pi = 3.14159;
double area = pi * radius * radius;
double circumference = 2 * pi * radius;
System.out.println("Area of circle is " + area);
System.out.println("Circumference of circle is " + circumference);
}
}
Area of circle is 78.53975
Circumference of circle is 31.4159
radius
and initialize it with the value 5, which represents the radius of the circle. We also declare another double variable pi
and initialize it with the value of Pi
, which is approximately 3.14159. Pi
by the radius squared, and store the result in a double variable area
. Pi
by the radius, and store the result in a double
variable circumference. import java.util.Scanner;
class CircleDemo
{
static Scanner sc = new Scanner(System.in);
public static void main(String args[])
{
System.out.print("Enter the radius: ");
/*We are storing the entered radius in double
* because a user can enter radius in decimals
*/
double radius = sc.nextDouble();
//Area = PI*radius*radius
double area = Math.PI * (radius * radius);
System.out.println("The area of circle is: " + area);
//Circumference = 2*PI*radius
double circumference= Math.PI * 2*radius;
System.out.println( "The circumference of the circle is:"+circumference) ;
}
}
Enter the radius: 7
The area of circle is: 153.93804002589985
The circumference of the circle is:43.982297150257104