Here we are explaining how to calculate the area of a rectangle.
A rectangle has four sides where two adjacent sides are equal. The area of a rectangle is calculated by multiplying the length of the rectangle by the width of the rectangle.
Area of rectangle=l*w
where l is the length of the rectangle and w is the width of the rectangle.
First, we have to declare the class AreaRect.Create an object of the scanner class as sc and read the length and width of the rectangle from the user into the variable l,w using nextDouble() method.Calulate the area as a=l*w.Display area as a by using system.out.println()
.
STEP 1: Declare the class AreaRect with a public modifier.
STEP 2: Open the main() to start the program, Java program execution starts with the main()
STEP 3: Declare the variables l,w,a as double.
STEP 4: Read the length of the rectangle as l.
STEP 5: Read the width of the rectangle as w.
STEP 6: Calculate the area of rectangle as a=l*w.
STEP 7: Display area a.
import java.util.Scanner;
public class AreaRect{
public static void main(String args[]) {
double l,w,a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the Rectangle:");
l = sc.nextDouble();
System.out.println("Enter the width of the Rectangle:");
w = sc.nextDouble();
a = l*w;
System.out.println("The Area of the Rectangle is: " + a);
}
}
Enter the length of the Rectangle:10 Enter the width of the Rectangle:20 The Area of the Rectangle is:200.0