Table of contents
1.
Introduction
2.
Formula for Area of Rectangle and Perimeter of Rectangle
3.
Algorithm  
4.
Approach
4.1.
1. Using Static Values
4.2.
2. Using User Input (Scanner Class)
4.3.
3. Using Methods
4.4.
4. Using Object-Oriented Programming (OOP)
5.
Time and Space Complexity
6.
Frequently Asked Questions
6.1.
What is the formula to calculate the area and perimeter of a rectangle?
6.2.
What is the difference between using static values and user input in Java?
6.3.
Why is OOP preferred over other approaches in Java?
7.
Conclusion
Last Updated: Mar 17, 2025
Medium

Program to calculate the area of rectangle in Java

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Calculating the area of a rectangle in Java can be done using the length and width of the rectangle. This calculation is useful in various mathematical and real-world applications where determining the space occupied by a rectangular shape is required. 

In this article, we will learn different methods to achieve this calculation, along with examples to demonstrate their implementation.

Formula for Area of Rectangle and Perimeter of Rectangle

Before writing the Java program, let's understand the formulas used to calculate the area and perimeter of a rectangle:

  • Area of a Rectangle = length × width
     
  • Perimeter of a Rectangle = 2 × (length + width)

These formulas help in determining the space covered by the rectangle and its boundary length.

Algorithm  

Before jumping into writing code, it’s important to understand the steps or algorithm required to calculate the area of a rectangle. An algorithm is like a recipe—it tells you what to do step by step. Let’s discuss this in detail:  

1. Understand the Formula:  

The area of a rectangle is calculated using the formula:  

  Area = Length × Width 


Here, "Length" is one side of the rectangle, & "Width" is the side perpendicular to it.  


2. Take Input:  

To calculate the area, we need the values of length & width. These values can be provided by the user or hardcoded in the program.  


3. Perform the Calculation:  

Multiply the length by the width to get the area.  


4. Display the Result:  

Show the calculated area to the user.  


For example: 


import java.util.Scanner; // Import the Scanner class to take user input

public class RectangleArea {
    public static void main(String[] args) {
        // Step 1: Create a Scanner object to read input
        Scanner scanner = new Scanner(System.in);


        // Step 2: Ask the user for the length & width
        System.out.print("Enter the length of the rectangle: ");
        double length = scanner.nextDouble(); // Read the length


        System.out.print("Enter the width of the rectangle: ");
        double width = scanner.nextDouble(); // Read the width


        // Step 3: Calculate the area
        double area = length  width;


        // Step 4: Display the result
        System.out.println("The area of the rectangle is: " + area);


        // Close the scanner to free up resources
        scanner.close();
    }
}
You can also try this code with Online Java Compiler
Run Code


In this Code:  

1. Importing Scanner:  

We use the `Scanner` class to take input from the user. It’s part of the `java.util` package, so we import it at the beginning.  


2. Creating a Scanner Object:  

The `Scanner` object (`scanner`) is created to read input from the keyboard.  
 

3. Taking User Input:  

  • We prompt the user to enter the length & width using `System.out.print()`.  
     
  • The `nextDouble()` method reads the input as a decimal number.  


4. Calculating the Area:  

The formula `length  width` is used to compute the area, & the result is stored in the `area` variable.  


5. Displaying the Result:  

The result is printed using `System.out.println()`.  


6. Closing the Scanner:  

It’s good practice to close the `Scanner` object to free up system resources.  

Approach

We can follow multiple approaches to calculate the area of a rectangle in Java:

  1. Using Static Values - Assigning fixed values to length and width.
     
  2. Using User Input (Scanner Class) - Taking input from the user for length and width.
     
  3. Using Methods - Creating a method that takes parameters and returns the area.
     
  4. Using Object-Oriented Programming (OOP) - Using a class and objects to encapsulate the logic.
     

Let's look at each approach with proper Java programs.

1. Using Static Values

public class RectangleArea {
    public static void main(String[] args) {
        int length = 10;
        int width = 5;
        int area = length * width;
        int perimeter = 2 * (length + width);
        System.out.println("Area of the rectangle: " + area);
        System.out.println("Perimeter of the rectangle: " + perimeter);
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Area of the rectangle: 50
Perimeter of the rectangle: 30

2. Using User Input (Scanner Class)

import java.util.Scanner;
public class RectangleInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the length of the rectangle: ");
        int length = sc.nextInt(); 
        System.out.print("Enter the width of the rectangle: ");
        int width = sc.nextInt();
        int area = length * width;
        int perimeter = 2 * (length + width);
        System.out.println("Area of the rectangle: " + area);
        System.out.println("Perimeter of the rectangle: " + perimeter);
        sc.close();
    }
}
You can also try this code with Online Java Compiler
Run Code


Output 

Enter the length of the rectangle: 8
Enter the width of the rectangle: 4
Area of the rectangle: 32
Perimeter of the rectangle: 24

3. Using Methods

import java.util.Scanner;

public class RectangleMethod {
    public static int calculateArea(int length, int width) {
        return length * width;
    } 
    public static int calculatePerimeter(int length, int width) {
        return 2 * (length + width);
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter length: ");
        int length = sc.nextInt();
        System.out.print("Enter width: ");
        int width = sc.nextInt();
        System.out.println("Area: " + calculateArea(length, width));
        System.out.println("Perimeter: " + calculatePerimeter(length, width));
        sc.close();
    }
}
You can also try this code with Online Java Compiler
Run Code

4. Using Object-Oriented Programming (OOP)

import java.util.Scanner;
class Rectangle {
    int length, width;
    Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
    }
    
    int getArea() {
        return length * width;
    }  
    int getPerimeter() {
        return 2 * (length + width);
    }
}
public class RectangleOOP {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter length: ");
        int length = sc.nextInt();  
        System.out.print("Enter width: ");
        int width = sc.nextInt(); 
        Rectangle rect = new Rectangle(length, width);
        System.out.println("Area: " + rect.getArea());
        System.out.println("Perimeter: " + rect.getPerimeter()); 
        sc.close();
    }
}
You can also try this code with Online Java Compiler
Run Code

Time and Space Complexity

ApproachTime ComplexitySpace Complexity
Static ValuesO(1)O(1)
User InputO(1)O(1)
MethodsO(1)O(1)
OOPO(1)O(1)

Since all approaches involve basic arithmetic operations and a few variable assignments, the time complexity remains O(1) (constant time). The space complexity is also O(1) since only a few integer variables are used.

Frequently Asked Questions

What is the formula to calculate the area and perimeter of a rectangle?

The formula for the area is length × width, and for the perimeter, it is 2 × (length + width).

What is the difference between using static values and user input in Java?

Using static values means assigning fixed numbers in the program, while user input allows dynamic values to be entered when running the program.

Why is OOP preferred over other approaches in Java?

OOP provides better code organization, reusability, and encapsulation, making it useful for larger projects.

Conclusion

In this article, we discussed different ways to calculate the area and perimeter of a rectangle in Java. We used static values, user input, methods, and object-oriented programming to implement the logic. Additionally, we analyzed the time and space complexity of each approach. This knowledge will help you in understanding Java fundamentals, user input handling, and method usage effectively. 

Live masterclass