Java Program to Find Area of Rectangle

The area of the Rectangle is given by length * width. Here we will write a Java program to find the area of the rectangle. We will take length and width variables to store the value of length and width respectively.

Area of Rectangle = length * width

Procedures to write a Java program to calculate the area of the rectangle:-

1) Define class and the main method
2) Declare variables for taking inputs:- width and height
3) Import Scanner class of util package to read inputs
4) Read inputs from end-user and store them in the declared variables
5) Calculate area using the formula, and store it in a variable
6) Display the result
7) Close the Scanner class object

The value of width and height may be an integer or floating-point value. So, it is a better idea to take width and length variable as double data type, because it can store both types of values. The final result will be a floating-point number hence, declare the area as the double data type.

import java.util.Scanner;

public class RectangleArea {

    public static void main(String[] args) {

        // declare variables 
        double length = 0.0;
        double width = 0.0;
        double area = 0.0;

        // create Scanner class object to read input
        Scanner scan = new Scanner(System.in);

        // read input
        System.out.print("Enter length of the rectangle:: ");
        length = scan.nextDouble();
        System.out.print("Enter width of the rectangle:: ");
        width = scan.nextDouble();

        // calculate area
        area = length * width;

        // display result
        System.out.println("Area of Rectangle = "+ area);

        // close Scanner class object
        scan.close();

     }
 }

Output:-

Enter length of the rectangle:: 14.5
Enter width of the rectangle:: 10
Area of Rectangle = 145.0

Area of the rectangle in Java using method

Previously In the Java program to find the area of Rectangle, we have written logic to find the area of the rectangle inside the main method. it was the wrong way. We should define a method to calculate the area of the rectangle and call it from the main method.

import java.util.Scanner;

public class RectangleArea {

  public static double findRectangleArea(double len, double wid) {
       return len * wid;
  }

  public static void main(String[] args) {

       // declare variables 
       double length = 0.0;
       double width = 0.0;
       double area = 0.0;

       // create Scanner class object to read input
       Scanner scan = new Scanner(System.in);

       // read input
       System.out.print("Enter length of the rectangle:: ");
       length = scan.nextDouble();
       System.out.print("Enter width of the rectangle:: ");
       width = scan.nextDouble();

       // calculate area by calling findRectangleArea()
       area = findRectangleArea(length, width);

       // display result
       System.out.printf("Area of Rectangle = %.2f", area);

       // close Scanner class object
       scan.close();

   }
 }

Output:-

Enter length of the rectangle:: 15.5
Enter width of the rectangle:: 12.8
Area of Rectangle = 198.40

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *