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.
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.
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
You may also Find this interesting
Program to find even or odd numbers
Program to Display average of numbers
Program to Display Fabinocci Series
Program to Display Random Numbers
Program to Display Largest of Three numbers