Write a Java program to input length and width of a rectangle and find area of the given rectangle. How to calculate area of a rectangle in Java programming. Logic to find area of a rectangle whose length and width are given in Java programming.
Required knowledge
Arithmetic operators, Data types, Basic input/output
Area of rectangle
Area of a rectangle is given by the formula -
Area=lw
Where l is length and w is width of the rectangle.
Logic to find area of rectangle
Below is the step by step descriptive logic to find area of rectangle -
Input length and width of rectangle. Store it in two different variables say length and width.
Apply formula to calculate rectangle area i.e. area = length * width.
Finally, print the value of area.
Program to find area of rectangle
/** * Java program to find area of rectangle */ import java.util.Scanner; class Test { public static void main(String args[]) { float length, width, area; Scanner op=new Scanner(System.in); /* * Input length and width of rectangle */ System.out.print("Enter length of rectangle: "); length=op.nextFloat(); System.out.print("Enter width of rectangle: "); width=op.nextFloat(); /* Calculate area of rectangle */ area = length * width; /* Print area of rectangle */ System.out.println("Area of rectangle = "+area+" sq. units "); } }
Output:
Enter length of rectangle: 5 Enter width of rectangle: 3 Area of rectangle = 15.0 sq. units