Java program to find area of rectangle
In this post, you will learn how to write a java program to find the area of a rectangle.
Area of rectangle is the region covered by the rectangle in a two-dimensional plane. A rectangle is a 2d shape which has four sides and four vertices. The area of a rectangle is the number of unit squares that can fit into a rectangle. If we know the width and height of a rectangle, we can easily calculate the area of a rectangle using the following formula -
Area = Width * Height
Program 1: Find area of rectangle
In the given program, we have defined the width and height of the rectangle and calculate the area of the rectangle by multiplying the width and height. We have assigned the area of the rectangle to the area variable and printed them.
// Java program to calculate
// area of rectangle
public class AreaOfRectangle {
public static void main (String[] args)
{
double length = 23.42;
double width = 72.12;
// calculating area
double area = length*width;
System.out.println("Area of Rectangle : "+area);
}
}
Output of the above code:
Area of Rectangle : 1689.0504000000003
Program 2: Find area of rectangle using user input
In the given example, we have asked the user to provide the length and width values and calculate the area of the rectangle by multiplying the width and height.
// Java program to calculate
// area of rectangle
import java.util.Scanner;
public class FindAreaOfRectangle {
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Please enter the width of Rectangle:");
double width = scanner.nextDouble();
// Calculating area of rectangle
double area = length*width;
System.out.println("Area of Rectangle : "+area);
}
}
Output of the above code:
Please enter the length of Rectangle:
6
Please enter the width of Rectangle:
7
Area of Rectangle : 42.0
Program 3: Find area of rectangle using function
Here, we have defined a function to calculate the area of rectangle. Here, we have overloaded the area function to calculate the area of rectangle for taking double or integer parameters.
import java.io.*;
class AreaRectangle {
void Area(double S, double T)
{
System.out.println("Area of the rectangle: "
+ S * T);
}
void Area(int S, int T)
{
System.out.println("Area of the rectangle: "
+ S * T);
}
}
public class FindAreaOfRect {
// Driver code
public static void main(String[] args)
{
// Creating object of Rectangle class
AreaRectangle obj = new AreaRectangle();
// Calling function
obj.Area(16, 91);
obj.Area(12.5, 31.3);
}
}
Output of the above code:
Area of the rectangle: 1456
Area of the rectangle: 391.25
Related Articles
Matrix multiplication in JavaElectricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number