Area of a Circle in Java

The formula for calculating area of a circle is,

Area = PI * r * r, where PI is a constant and r is the radius of the circle.

The following sample Java program calculates area of a circle. The radius of the circle is taken as user input from command line. The default Java language package contains a Math class which the following program uses for the value of PI.

import java.util.Scanner;

/**
 * Java program to calculate area of a circle
 * @author jj
 */
public class AreaOfCircle {
    
    public static void main(String[] args) {
        System.out.print("Enter radius of circle: ");
        Scanner sn = new Scanner(System.in);
        Double radius = sn.nextDouble();
        
        Double area = Math.PI * radius * radius;        
        System.out.println("Area = "+area);
    }
}

Do you have a programming problem that you are unable to solve? Please send us your problem and we will publish the solution on this site within 48 hours! Please email us.


Leave a Reply