1. Prompt the user to input a character and a real number, and output them. 2. I
ID: 3880707 • Letter: 1
Question
1. Prompt the user to input a character and a real number, and output them. 2. If the character is ‘A’ and the number is positive, calculate the area of a circle using the number as the radius, format to 2 decimal places. 3. If the character is ‘C’ and the number is positive, calculate the circumference of a circle using the number as the radius, format to 2 decimal places. 4. If the character is not ‘A’ or ‘C’, or the number is not positive, print error message. brary> ENGR 204 home> 142: Prog 2 Calculate Area&Circumference; zyBooks catalog Help ()Prompt the user to input a character and real number,and output thiem (T pts Enter a character: Enter a number: 5.2 The inputs are A and S.2 (2) if the character is A' and the number is positive, calculate the area of a circle using the number as the radus,format to 2 (3 pts) Area with radius 5.2 is 84.95 C and the number is positive, calculate the circumference of a circle using the number as the radius,format to 2 (2) if the character is decimal places (3 pts) Circumference with radius 5.2 1s 32.67 (3) if the character is not A or 'C'·or the number is not positive, print error message (3 pts) Invalid entry 0/10Explanation / Answer
import java.util.Scanner;
public class Circle
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println(" Enter a character: ");
char c = scan.next().charAt(0);
System.out.println(" Enter a number: ");
double number = scan.nextDouble();
System.out.println(" The Inputs are " + c + " and " + number);
if((c == 'A') && (number > 0))
{
double area = 3.14 * number * number;
System.out.printf(" Area with radius " + number + " is %.2f", area);
}
else if((c == 'C') && (number > 0))
{
double circum = 2 * 3.14 * number;
System.out.printf(" Circumference radius " + number + " is %.2f", circum);
}
else
{
System.out.println(" Invalid Entry");
}
}
}
OUTPUT
Enter a character: A
Enter a number: 5.2
The Inputs are A and 5.2
Area with radius 5.2 is 84.91
OUTPUT
Enter a character: C
Enter a number: 5.2
The Inputs are C and 5.2
Circumference radius 5.2 is 32.66
OUTPUT
Enter a character: X
Enter a number: 5.2
The Inputs are X and 5.2
Invalid Entry
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.