# How to calculate Area and Perimeter of Rectangle through a simple Java program ?

Updated on September 17, 2014

In this post I will be teaching you how to calculate Area and Perimeter of Rectangle through a simple Java program. I have also provided a video tutorial for the same. The source code for this simple Java program is listed below just after the video. In this tutorial calculation of area and perimeter of rectangle is done based on two simple formula as :

1. Area of Rectangle = length * breadth

2. Perimeter of Rectangle = 2 * (length + breadth).

In the above two formula, length and breadth are the dimensions of rectangle.

## Area and Perimeter of the Rectangle in Java through Scanner.

```package com.hubberspot.example;

import java.util.Scanner;

public class RectangleDemo {

public static void main(String[] args) {

// Step 1 --- Create a Scanner object,
// it will take System.in object in its constructor.
// Its a inputstream which provide Scanner
// capability of reading user input from console.

Scanner scanner = new Scanner(System.in);

// Step 2 --- Ask the user to enter the
// value of length and breadth of Rectangle.

System.out.print("Enter the Length of Rectangle --- ");

// Step 3 --- User enters Length of Rectangle on the console
// and it is been read by scanner object by the method
// nextDouble() and return value is been stored in the length
// variable

double length = scanner.nextDouble();

// Step 4 --- Ask the user to enter the
// value of length and breadth of Rectangle.

System.out.print("Enter the Breadth of Rectangle --- ");

// Step 5 --- User enters Breadth of Rectangle on the console
// and it is been read by scanner object by the method
// nextDouble() and return value is been stored in the breadth
// variable

// Step 6 --- Two variable created for storing the area and
// perimeter of Rectangle.

double area = 0;
double perimeter = 0;

System.out.println("Calculating Area and Perimeter of Rectangle ---- ");

// Step 7 --- Formula for Area of Rectangle is length * breadth
// Area is calculated and stored in the area variable

// Step 8 --- Formula for Perimeter of Rectangle is 2 * (length + breadth)
// Perimeter is calculated and stored in the perimeter variable

perimeter = 2 * (length + breadth);

// Step 9 --- Output is displayed on the console

System.out.println("The Area of Rectangle is --- " + area);
System.out.println("The Perimeter of Rectangle is --- " + perimeter);
}

}
```

0

9

0

0

6

3