C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Area of a circle - Java Program

Formula used:

Area = π × r2

Where:

  • π (pi) = 3.14159 (or Java’s Math.PI)
  • r = radius of the circle

 

import java.util.Scanner;

 

public class AreaOfCircle {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

        // Taking radius input

        System.out.print("Enter the radius of the circle: ");

        double radius = sc.nextDouble();

 

        // Calculating area using formula

        double area = Math.PI * radius * radius;

 

        // Displaying area

        System.out.println("The area of the circle is: " + area);

 

        sc.close();

    }

}

Output

 
OUTPUT 1:
Enter the radius of the circle: 5
The area of the circle is: 78.53981633974483

OUTPUT 2:
Enter the radius of the circle: 2.5
The area of the circle is: 19.634954084936208

Explanation

Step 1 — Input radius

double radius = sc.nextDouble();

Accepts radius as a floating-point number.

Step 2 — Apply Area Formula

double area = Math.PI * radius * radius;

  • Math.PI gives the most accurate value of π
  • radius * radius computes r2
  • Final result stored in area

Step 3 — Display the Area

System.out.println("The area of the circle is: " + area);