Area of rectangle java program

Hey guys!! Welcome to flower brackets blog. Today we are going to learn area of rectangle java program.

area of rectangle java program

In this java program user will be prompted to enter the length and width of a rectangle. Then using input values we calculate the area.

Also read – calculate perimeter of rectangle in java

// Area = length * width 

import java.util.Scanner;

public class RectangleArea 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter length of rectangle : ");
      double length = sc.nextDouble();
      System.out.println("Please enter width of rectangle : ");
      double width = sc.nextDouble();
      double area = length * width;
      System.out.println("Area : " + area);
      sc.close();
   }
}

Output:

Please enter length of rectangle : 2
Please enter width of rectangle : 5
Area : 10.0


Similarly create a function which computes the area of rectangle using two parameters length and width.

Then the value “area” is returned to the main function from where it is called. Let’s see the code execution,

import java.util.*;
 
public class AreaRectangle
{ 
   public static void main(String []args)
   {
      double length, width, area;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter length of rectangle : ");
      length = sc.nextDouble();
      System.out.print("Please enter width of rectangle : ");
      width = sc.nextDouble();
      area = areaOfRectangle(length, width);
      System.out.println("Area : " + area);
      sc.close();
   }
 
   public static double areaOfRectangle(double l, double w)
   {
      double area;
      area = l * w;
      return area;
   }
}

Output:

Please enter length of rectangle : 60
Please enter width of rectangle : 40
Area : 2400.0

Related Posts