πŸ” Java How-To Examples
Estimated reading: 2 minutes 27 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Area of Rectangle

Or Copy Link

CONTENTS
Scroll to Top