Java Program to Calculate Area of Rectangle

Java Program to Calculate Area of Rectangle

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

Post a Comment

Previous Post Next Post