🔍 Java How-To Examples
Estimated reading: 2 minutes 271 views

Java Program to Calculate Area of a Rectangle – With Examples

Introduction

Calculating the area of a rectangle is one of the simplest and most common geometric operations in Java. Whether you’re building a geometry calculator, GUI layout system, or floor planner, this logic is essential.

By the end of this article, you’ll learn:

  • How to compute the area of a rectangle in Java
  • Take user input using Scanner
  • Write clean and reusable Java methods

Formula for Area of Rectangle

Area = Length × Breadth

Both values must be positive numbers, typically in int or double format.


Java Program – Hardcoded Input

public class RectangleArea {
    public static void main(String[] args) {
        int length = 10;
        int breadth = 5;

        int area = length * breadth;

        System.out.println("Area of rectangle = " + area);
    }
}

Explanation:

  • length and breadth are initialized directly.
  • area = length * breadth applies the rectangle area formula.
  • Output is printed with System.out.println.

Java Program – User Input with Scanner

import java.util.Scanner;

public class RectangleAreaInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter length: ");
        double length = scanner.nextDouble();

        System.out.print("Enter breadth: ");
        double breadth = scanner.nextDouble();

        double area = length * breadth;

        System.out.println("Area of rectangle = " + area);
    }
}

Explanation:

  • Scanner reads double input from user.
  • Calculates area using multiplication.
  • Works for both integers and decimal inputs.

Always close the Scanner using scanner.close() in production code.


Java Method – Reusable Function

public class RectangleUtils {
    public static double calculateArea(double length, double breadth) {
        return length * breadth;
    }

    public static void main(String[] args) {
        double area = calculateArea(8.5, 4.2);
        System.out.println("Area = " + area);
    }
}

Explanation:

  • calculateArea() is a reusable method.
  • Keeps code modular and clean.

Edge Case Warning

if (length <= 0 || breadth <= 0) {
    System.out.println("Length and breadth must be positive.");
    return;
}

Always validate input values to avoid incorrect or negative area calculations.


Example Output

Enter length: 6
Enter breadth: 3
Area of rectangle = 18.0

Summary

In this article, you’ve learned:

  • How to calculate rectangle area in Java using formula
  • Take user input with Scanner
  • Use methods for clean and reusable logic

This knowledge applies to many Java-based geometry, GUI, and simulation projects.


FAQ – Area of Rectangle in Java

Can I use float instead of double?

Yes, but double is preferred for higher precision.

How do I handle invalid input?

Use exception handling:

try {
    double length = scanner.nextDouble();
} catch (InputMismatchException e) {
    System.out.println("Invalid input! Please enter a number.");
}

Share Now :
Share

Area of Rectangle

Or Copy Link

CONTENTS
Scroll to Top