In this program, we are going to share a Java program to calculate rectangle area. If you are a Java beginner and want to start learning the Java programming, then keep your close attention in this tutorial as I am going to share how to write a Java program to calculate rectangle area.
Copy the below Java program and execute it with the help of Javac compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CalculateRectArea { public static void main(String[] args) { int width = 0; int length = 0; try { //read the length from console BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please enter length of a rectangle"); length = Integer.parseInt(br.readLine()); //read the width from console System.out.println("Please enter width of a rectangle"); width = Integer.parseInt(br.readLine()); } catch(NumberFormatException ne) { System.out.println("Invalid value" + ne); System.exit(0); } catch(IOException ioe) { System.out.println("IO Error :" + ioe); System.exit(0); } int area = length * width; System.out.println("Area of a rectangle is " + area); } } |
Please enter length of a rectangle
10
Please enter width of a rectangle
15
Area of a rectangle is 150
Liked this program? Do Like & share with your friends.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.