Google News
logo
Java Program to Calculate Area of Rectangle
In the following examples of Java program to calculate the area of a rectangle :
Program :
import java.util.Scanner;
class AreaOfRectangle {
   public static void main (String[] args)
   {
	   Scanner scanner = new Scanner(System.in);
	   System.out.println("Enter the length of Rectangle:");
	   double length = scanner.nextDouble();
	   System.out.println("Enter the width of Rectangle:");
	   double width = scanner.nextDouble();
	   //Area = length*width;
	   double area = length*width;
	   System.out.println("Area of Rectangle is:"+area);
   }
}
Output :
Enter the length of Rectangle:
2
Enter the width of Rectangle:
8
Area of Rectangle is:16.0
Example 2 :
Program :
public class RectangleArea {

    public static void main(String[] args) {
        double length = 10;
        double width = 5;
        double area = length * width;
        System.out.println("Area of rectangle is " + area);
    }
}
Output :
Area of rectangle is 50.0
In this program, we first declare two double variables length and width and initialize them with the values 10 and 5 respectively. We then calculate the area of the rectangle by multiplying the length and width, and store the result in a double variable area. Finally, we print the area of the rectangle using a formatted string.