Answer to Question #171060 in Java | JSP | JSF for jotish kumar

Question #171060

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.


1
Expert's answer
2021-03-12T04:21:47-0500
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.");
        }
    }
} 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog