Write a program that requests an input (P for perimeter and A for area) then asks the user for the dimensions (length & width) of a rectangular pool. The program computes and displays either the perimeter or the area of the pool.
Since you did not mention the language, Here is the completed code for this problem in Java. If it is any other language, just mention in comments. Also, don’t forget to include the language next time you post a question. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// scanner to read user input
Scanner scanner = new Scanner(System.in);
// asking the user to enter P or A
System.out
.println("What do you want to calculate? Enter P for perimeter or A for area: ");
// reading choice, converting to upper case and extracting first
// character
char choice = scanner.next().toUpperCase().charAt(0);
// if choice is 'P' or 'A', proceeding to read dimensions
if (choice == 'P' || choice == 'A') {
System.out.println("Enter length and width of the rectangle: ");
// reading dimensions
double length = scanner.nextDouble();
double width = scanner.nextDouble();
// if choice is 'P', finding and displaying perimeter
if (choice == 'P') {
// formula = 2(length+width)
double peri = 2 * (length + width);
System.out.println("Perimeter is " + peri);
}
// otherwise finding and displaying area
else {
//formula = length x width
double area = length * width;
System.out.println("Area is " + area);
}
} else {
//any other input is invalid
System.out.println("Invalid choice!");
}
}
}
/*OUTPUT 1*/
What do you want to calculate? Enter P for perimeter or A for area:
P
Enter length and width of the rectangle:
10 20
Perimeter is 60.0
/*OUTPUT 2*/
What do you want to calculate? Enter P for perimeter or A for area:
a
Enter length and width of the rectangle:
100 50
Area is 5000.0
Get Answers For Free
Most questions answered within 1 hours.