Input the circumference of a circle and print the radius and area. The answers should be decimal values and must be printed in the format shown in the sample run. Remember the formula for circumference is 2 * pi * r and the formula for area is pi * r * r. You can use 3.14 for the value of pi in this activity. Sample Run:

in java

1 answer

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:

  1. Import Scanner: Import the Scanner class to take input from the user.
  2. Prompt for Circumference: The program prompts the user to enter the circumference of the circle.
  3. Calculate Radius: Using the formula \( r = \frac{C}{2 \pi} \) where C is the circumference and pi is 3.14.
  4. Calculate Area: Using the formula \( \text{Area} = \pi r^2 \).
  5. 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.