The area of a rectangle is the rectangle’s length times its width. Write a program that asks for the length and width of two rectangles. The program should tell the user which rectangle has the greater area, or if the areas are the same.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int firstLength;
int firstWidth;
int firstArea;
int secondLength;
int secondWidth;
int secondArea;
Scanner myObj = new Scanner(System.in);
System.out.println("Enter the length of the first rectangle:");
firstLength = myObj.nextInt();
System.out.println("Enter the width of the first rectangle:");
firstWidth = myObj.nextInt();
System.out.println("Enter the length of the second rectangle:");
secondLength = myObj.nextInt();
System.out.println("Enter the width of the second rectangle:");
secondWidth = myObj.nextInt();
firstArea = firstLength * firstWidth;
secondArea = secondLength * secondWidth;
if ( firstArea > secondArea ) {
System.out.println("The first rectangle has the greater area.");
} else if ( firstArea < secondArea ) {
System.out.println("The second rectangle has the greater area.");
} else {
System.out.println("The areas of rectangles are the same.");
}
}
}
Comments
Leave a comment