Rectangle java program

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

rectangle java program

In this java program user will be prompted to enter the length and width of a rectangle. Then using this 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("Enter length of rectangle : ");
      double length = sc.nextDouble();
      System.out.println("Enter width of rectangle : ");
      double width = sc.nextDouble();
      double area = length * width;
      System.out.println("Area : " + area);
      sc.close();
   }
}

Output:

Enter length of rectangle : 2
Enter width of rectangle : 5
Area : 10.0


Similarly create a function which computes the area 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("Enter length of rectangle : ");
      length = sc.nextDouble();
      System.out.print("Enter width of rectangle : ");
      width = sc.nextDouble();
 
      area = AreaOfRectangle(length, width);

      System.out.println("Area : " + area);
      sc.close();
   }
 
   public static double AreaOfRectangle(double length, double width)
   {
      double area;
      area = length * width;
      return area;
   }
}

Output:

Enter length of rectangle : 60

Enter width of rectangle : 40

Area : 2400.0


Related Posts