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

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

In java program to find area of rectangle user will be prompted to enter the length and width of a rectangle using nextDouble() method of Scanner class.

Also read – program to calculate area and perimeter of rectangle

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

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: 5
Please enter width of rectangle: 2
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 : 8
Please enter width of rectangle : 6
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 square

Now let’s learn to calculate area of square. Here’s the java program to calculate area of square,

import java.util.Scanner;

public class FindAreaOfSquare 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter side of square: ");
      double side = sc.nextDouble();
      // formula area of square = side * side
      double area = side * side; 
      System.out.println("Area of square is: " + area);
      sc.close();
   }
}


Output:

Please enter side of square: 5
Area of square is: 25.0


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