Java - Input the length and breadth of a rectangle and find the area

By IncludeHelp Last updated : April 7, 2024

Problem statement

Write a program to input the length and breadth of a rectangle and find the area.

Example

Input:
Length: 2.3 Breadth: 4.5
Area of Rectangle: 10.349999

Finding the area of the rectangle

To find the area rectangle, input the length and breadth of the rectangle and then multiply them as per the formula of the rectangle. To input the length and breadth in float, use the nextFloat() method of the Scanner class.

Java program to Input the length and breadth of a rectangle and find the area

import java.util.Scanner; // Import the Scanner class

public class Main {
  public static void main(String[] args) {
    // Create a Scanner object to input 
    Scanner myObj = new Scanner(System.in);

    System.out.println("Input length: ");
    float length = myObj.nextFloat(); // Read float
    System.out.println("Input breadth: ");
    float breadth = myObj.nextFloat(); // Read float    

    float areaOfRectangle = length * breadth;

    System.out.println("Area of rectangle is: " + areaOfRectangle);

  }
}

Output

The output of the above program is:

RUN 1:
Input length: 
2.3
Input breadth: 
4.5
Area of rectangle is: 10.349999


RUN 2:
Input length: 
1.23
Input breadth: 
5.5
Area of rectangle is: 6.7650003

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.