A Program to Find the Area of a Rectangle

In this example, we are giving a program to find the area of a rectangle. The rule of finding the area of a rectangle is: A = width * height.


Program to find the area of a rectangle:

  1. import java.util.Scanner; //Scanner class helps to get input from user
  2. class triangle {
  3. public static void main(String[] args) {
  4. Scanner s = new Scanner(System.in); //Scanner class object declaration
  5. System.out.println("Enter the width of rectangle: ");
  6. float w = s.nextFloat(); // takes float input width from keyboard or user or run time
  7. System.out.println("Enter the height of rectangle: ");
  8. float h = s.nextFloat(); // takes float input height from keyboard or user or run time
  9. float area = w * h;
  10. System.out.println("Area of Rectangle: "+ area);
  11. }
  12. }

Output:

Enter the width of rectangle: 3
Enter the height of rectangle: 4
Area of Rectangle: 12.00