Description
This basic java example source code reads length and width of a rectangle from console input and then calculate the area and perimeter.
Rectangle Calculate Area and Perimeter Source Code
package com.javatutorialhq.java.examples; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /* * This basic java example source code * reads width and length of a rectangle * Java Program to calculate the area and perimeter of rectangle */ public class RectangleCalculation { public static void main(String[] args) throws NumberFormatException, IOException { // Declare the reader from console BufferedReader br; // Get the console input for the rectangle dimension System.out.println("Enter the Dimension of the Rectangle"); System.out.print("L:"); br = new BufferedReader(new InputStreamReader(System.in)); // assign to float variable the length console input float length = Float.parseFloat(br.readLine()); System.out.print("W:"); br = new BufferedReader(new InputStreamReader(System.in)); // assign to float variable the width console input float width = Float.parseFloat(br.readLine()); // calculate the area of rectangle float area = length * width; // calculate the perimeter of rectangle float perimeter = 2 * (length + width); System.out.println("Area is "+ area); System.out.println("Perimeter is "+perimeter); } }
Sample Output
Enter the Dimension of the Rectangle L:13.2 W:12.05 Area is 159.06 Perimeter is 50.5