➤ Hello World program
➤ Java Addition Program
➤ Average of 2 Numbers
➤ Average of 3 Numbers
➤ Simple Interest in Java
➤ Compound Interest
➤ Display ASCII Value
➤ Area of Circle Java
➤ Area of Rectangle
➤ Java Area of Triangle
➤ Java Swap 2 Numbers
➤ Distance b/w 2 Points
Java Flow Control Program
Java Array Programs
Java String Programs
The area of the Rectangle is given by length * width. Here we will write a Java program to find the area of the rectangle. We will take length and width variables to store the value of length and width respectively.
Area of Rectangle = length * width
Procedures to write a Java program to calculate the area of the rectangle:-
1) Define class and the main method
2) Declare variables for taking inputs:- width and height
3) Import Scanner class of util package to read inputs
4) Read inputs from end-user and store them in the declared variables
5) Calculate area using the formula, and store it in a variable
6) Display the result
7) Close the Scanner class object
The value of width and height may be an integer or floating-point value. So, it is a better idea to take width and length variable as double data type, because it can store both types of values. The final result will be a floating-point number hence, declare the area as the double data type.
import java.util.Scanner;
public class RectangleArea {
public static void main(String[] args) {
// declare variables
double length = 0.0;
double width = 0.0;
double area = 0.0;
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter length of the rectangle:: ");
length = scan.nextDouble();
System.out.print("Enter width of the rectangle:: ");
width = scan.nextDouble();
// calculate area
area = length * width;
// display result
System.out.println("Area of Rectangle = "+ area);
// close Scanner class object
scan.close();
}
}
Output:-
Enter length
of the rectangle:: 14.5
Enter width
of the rectangle:: 10
Area of Rectangle = 145.0
Area of the rectangle in Java using method
Previously In the Java program to find the area of Rectangle, we have written logic to find the area of the rectangle inside the main method. it was the wrong way. We should define a method to calculate the area of the rectangle and call it from the main method.
import java.util.Scanner;
public class RectangleArea {
public static double findRectangleArea(double len, double wid) {
return len * wid;
}
public static void main(String[] args) {
// declare variables
double length = 0.0;
double width = 0.0;
double area = 0.0;
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter length of the rectangle:: ");
length = scan.nextDouble();
System.out.print("Enter width of the rectangle:: ");
width = scan.nextDouble();
// calculate area by calling findRectangleArea()
area = findRectangleArea(length, width);
// display result
System.out.printf("Area of Rectangle = %.2f", area);
// close Scanner class object
scan.close();
}
}
Output:-
Enter length
of the rectangle:: 15.5
Enter width
of the rectangle:: 12.8
Area of Rectangle = 198.40
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!