Find Length and Breadth of Rectangle in Java


Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter


The program begins by creating a Scanner object to read input from the user. It then prompts the user to enter the length and breadth of the rectangle using the nextFloat() method of the Scanner class.

Next, the program calculates the area of the rectangle by multiplying its length and breadth and stores it in the area variable. It also calculates the perimeter of the rectangle by adding the lengths of all its sides and stores it in the perimeter variable.

Finally, the program prints the area and perimeter of the rectangle using the println() method of the System.out object. It then checks which one is greater using an if-else statement and prints a message accordingly.

Source Code

import java.util.Scanner;
class Rectangle
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter The Length  :");
		float len = input.nextFloat();
		System.out.print("Enter The Breadth :");
		float bre = input.nextFloat();
		float area = len * bre;
		float perimeter = 2 * (len+bre);
		System.out.println("Area of Rectangle : "+ area);
		System.out.println("Perimeter of Rectangle : "+ perimeter);
		if(area>perimeter) 
		{
			System.out.println("Area of rectangle is greater than  Perimeter");
		}
		else
		{
			System.out.println("Perimeter of rectangle is greater than Area");
		}
	}
}

Output

Enter The Length  :12
Enter The Breadth :23
Area of Rectangle : 276.0
Perimeter of Rectangle : 70.0
Area of rectangle is greater than  Perimeter

Example Programs