Certainly! Below is a Java program that takes the circumference of a circle as input, calculates the radius and area, and prints the results in the specified format. It uses 3.14 as the value of pi.
import java.util.Scanner;
public class CircleCalculations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user for the circumference
System.out.print("Enter the circumference of the circle: ");
double circumference = scanner.nextDouble();
// Value of pi
double pi = 3.14;
// Calculate radius (r = circumference / (2 * pi))
double radius = circumference / (2 * pi);
// Calculate area (area = pi * r^2)
double area = pi * radius * radius;
// Print results
System.out.printf("Radius: %.2f\n", radius);
System.out.printf("Area: %.2f\n", area);
scanner.close();
}
}
Explanation of the Code:
- Import Scanner: Import the
Scanner
class to take input from the user. - Prompt for Circumference: The program prompts the user to enter the circumference of the circle.
- Calculate Radius: Using the formula \( r = \frac{C}{2 \pi} \) where C is the circumference and pi is 3.14.
- Calculate Area: Using the formula \( \text{Area} = \pi r^2 \).
- Print Results: Using
System.out.printf
to format the output to two decimal places.
How to Run:
- Compile the Java program using a Java compiler.
- Run the compiled class file and enter a value for the circumference when prompted.
- It will output the radius and area in the specified format.
You can copy and paste this code into a Java IDE or online Java compiler to test it.