Write a program that computes the area of a circular region (the shaded area in the diagram), given the radii of the inner and the outer circles, ri and ro, respectively.
import java.util.Scanner;
public class Q175874 {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
// Create Scanner object
Scanner keyboard = new Scanner(System.in);
try {
// Get the radius of the inner circle from the user
System.out.print("Enter the radius of the inner circle: ");
double ri = keyboard.nextDouble();
double ro=-1;
while(ro<ri) {
//Get the radius of the outer circle from the user
System.out.print("Enter the radius of the outer circle: ");
ro = keyboard.nextDouble();
}
//Calculate the area the inner circle
double areaInnerCircle=Math.PI*ri*ri;
//Calculate the area the outer circle
double areaOuterCircle=Math.PI*ro*ro;
double areaCircularRegion=areaOuterCircle-areaInnerCircle;
System.out.println("The area of a circular region: "+areaCircularRegion);
} catch (Exception e) {
// Display error message
System.out.println(e.getMessage());
}
// close Scanner
keyboard.close();
}
}
Comments
Leave a comment