Area of Rectangle in Java

Calculate the area of a rectangle given its length and breadth.

BeginnerModule 1: Basic Java ProgramsExample 10 of 20
area-of-rectangle.java
1import java.util.Scanner;
2
3public class Main {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6
7 System.out.print("Enter length: ");
8 double length = sc.nextDouble();
9 System.out.print("Enter breadth: ");
10 double breadth = sc.nextDouble();
11
12 double area = length * breadth;
13 System.out.println("Area of rectangle = " + area);
14
15 sc.close();
16 }
17}

Output

Enter length: 4
Enter breadth: 3
Area of rectangle = 12.0

What's going on

We apply the formula area = length × breadth.