BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Calculate Area of Rectangle

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial we will see how to calculate Area of Rectangle.

Program 1:
User would provide the length and width values during execution of the program and the area would be calculated based on the provided values.

/**
 * @author: BeginnersBook.com
 * @description: Program to Calculate Area of rectangle
 */
import java.util.Scanner;
class AreaOfRectangle {
   public static void main (String[] args)
   {
	   Scanner scanner = new Scanner(System.in);
	   System.out.println("Enter the length of Rectangle:");
	   double length = scanner.nextDouble();
	   System.out.println("Enter the width of Rectangle:");
	   double width = scanner.nextDouble();
	   //Area = length*width;
	   double area = length*width;
	   System.out.println("Area of Rectangle is:"+area);
   }
}

Output:

Enter the length of Rectangle:
2
Enter the width of Rectangle:
8
Area of Rectangle is:16.0

Program 2:
In the above program, user would be asked to provide the length and width values. If you do not need user interaction and simply want to specify the values in program, refer the below program.

/**
 * @author: BeginnersBook.com
 * @description: Calculation with no user interaction
 */
class AreaOfRectangle2 {
   public static void main (String[] args)
   {
	   double length = 4.5;
	   double width = 8.0;
	   double area = length*width;
	   System.out.println("Area of Rectangle is:"+area);
   }
}

Output:

Area of Rectangle is:36.0

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java program to reverse a number using for, while and recursion
  3. Java Program to Add Two Matrix using Multi-dimensional Arrays
  4. java program to find factorial of a given number using recursion
  5. Java Program to Reverse a String using Recursion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. mothusi says

    November 6, 2015 at 9:54 AM

    what if i want it to calculate 3 different areas of rectangles

    Reply
    • suman says

      March 24, 2016 at 7:09 PM

      how can you have 3 different areas in a rectangle?
      A rectangle is a four-sided flat shape where every angle is a right angle (90°).

      Reply

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap