Java program to calculate area of rectangle

Let’s learn java program to calculate area of rectangle.

Java program to calculate area of rectangle

Java program to calculate area of rectangle is the most common java program interview question.

To calculate area of rectangle user will be prompted to enter the length and width of a rectangle using nextDouble() method of Scanner class.

java program to calculate area of rectangle

This length and width of a rectangle values are stored in variables “length” and “width” of data type double.

Also read – program to calculate area and perimeter of rectangle

Then using above user entered values we calculate area of rectangle using below formula,

calculate rectangle 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 of rectangle is: " + area);
      sc.close();
   }
}


Output:

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


Above program can be executed without user interaction like this.

class RectangleArea
{
   public static void main (String[] args)
   {
      double length = 2.0;
      double width = 5.0;
      double area = length * width;
      System.out.println("Area of rectangle is: " + area);
   }
}


Output:

Area of rectangle is: 10.0


Similarly let’s find area of rectangle in java using method or java program to find area of rectangle using functions.

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


Output:

Please enter length of rectangle : 6
Please enter width of rectangle : 8
Area of rectangle = 48.0


Similarly let’s calculate area of rectangle in java using class and object or find the area of rectangle using class and object – click here.


Also read – java program to find area of rectangle using constructor


Java program to find area of triangle

Let’s learn java program to calculate area of triangle. Here’s the java program to find area of triangle.

import java.util.Scanner;
public class FindAreaOfTriangle 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter width of triangle: ");
      double base = sc.nextDouble();
      System.out.println("Please enter height of triangle: ");
      double height = sc.nextDouble();
      // formula area of triangle = (width * height) / 2
      double area = (base * height) / 2;
      System.out.println("Area of triangle is: " + area);
      sc.close();
   }
}


Output:

Please enter width of triangle: 5
Please enter height of triangle: 5
Area of triangle is: 12.5


Also read – java program to calculate area of circle

Also read – java program to find area of rectangle using constructor