Google News
logo
Java Program to Calculate Area and Circumference of Circle
In the following examples of Java program to calculate the area and circumference of a circle :
Program :
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);
    }
}
Output :
Area of circle is 78.53975
Circumference of circle is 31.4159
In this program, we first declare a double variable 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.

We then calculate the area of the circle by multiplying Pi by the radius squared, and store the result in a double variable area.

We also calculate the circumference of the circle by multiplying 2 by Pi by the radius, and store the result in a double variable circumference.

Finally, we print both the area and circumference of the circle using formatted strings.


Example 2 :
Program :
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) ;
   }
}
Output :
Enter the radius: 7
The area of circle is: 153.93804002589985
The circumference of the circle is:43.982297150257104