Skip to main content

ProwessApps

Learn · Practice · Excel

Java Program to calculate the area of a circle.

Java Code Example — Basic Programs

ADVERTISEMENT

Java Program to calculate the area of a circle.

Objective

Write a Java program to calculate the area of a circle given its radius.

Algorithm / Approach

  1. Prompt the user to enter the radius of the circle.
  2. Calculate the area using the formula Area = 3.14 * r * r.
  3. Store the result in a double variable.
  4. Print the result.
Test.java
import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Radius:");
  int r = s.nextInt();
  double ar = 3.14*r*r;
  System.out.println("Area = "+ar);
 }
}

Expected Output

Enter Radius:7
Area = 153.86

Explanation of the Program

  • The program prompts the user for the radius r.
  • It calculates the area using the formula π × r2, approximated as 3.14 * r * r.
  • The resulting area is stored in a double variable ar.
  • The calculated area is then printed to the console.

Complexity

Time Complexity O(1)
Space Complexity O(1)

Common Mistakes

  • Using an integer for the area, which truncates the decimal part of the result.
  • Using r + r instead of r * r.
ADVERTISEMENT