Write a program in java to input the length and breadth of a rectangle and calculate its area and perimeter
Answers
/**
* Java program to find perimeter and area of a rectangle.
*/
import java.util.Scanner;
public class Rectangle {
public static void main(String[] args) {
float length, width, area, perimeter;
// Create scanner class object
Scanner in = new Scanner(System.in);
// Input length and width of rectangle
System.out.print("Enter length of rectangle: ");
length = in.nextFloat();
System.out.print("Enter width of rectangle: ");
width = in.nextFloat();
// Calculate perimeter of rectangle
perimeter = 2 * (length + width);
// Calculate area of rectangle
area = length * width;
// Print perimeter and area of rectangle
System.out.println("Perimeter of rectangle is " + perimeter + " units.");
System.out.println("Area of rectangle is " + area + " sq. units.");
}
}
Answer:
import java.util.*;
public class Rectangle
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
float l,b,a,p;
System.out.println("Enter the length of the rectangle");
l= in.nextFloat();
System.out.println("Enter the breadth of the rectangle");
b=in.nextFloat();
a=(l*b);
System.out.println("The area of the rectangle is =" +a);
p=2*(l+b);
System.out.println("The perimeter of the rectangle is="+p);
}
}
Explanation:
We should accept the values in float data type because either the length or the breadth or both may be in decimal point and if it is then the area and perimeter will also be in decimal point.
For a rectangle _
Area =(length ×breadth)
Perimeter =2×(length +breadth);