Area Of Rectangle in Java
Of course! Here's how you can find the Area of a Rectangle in Java:
Formula:
Example 1: Area of Rectangle (Fixed Values)
public class RectangleArea { public static void main(String[] args) { int length = 5; // Length of rectangle int breadth = 10; // Breadth of rectangle int area = length * breadth; // Area formula System.out.println("Area of Rectangle is: " + area); }}Output:
Area of Rectangle is: 50Example 2: Area of Rectangle (User Input)
Using Scanner to take input from the user:
import java.util.Scanner;public class RectangleAreaUserInput { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the length of the rectangle: "); int length = scanner.nextInt(); System.out.print("Enter the breadth of the rectangle: "); int breadth = scanner.nextInt(); int area = length * breadth; System.out.println("Area of Rectangle is: " + area); scanner.close(); }}Sample Output:
Enter the length of the rectangle: 7Enter the breadth of the rectangle: 4Area of Rectangle is: 28Would you also like me to show how to calculate the Perimeter of the Rectangle too? ??
(It's just a small addition!)