This post is about how we can find out the Area of Rectangle in java. Area of Rectangle in java follows the basic syntax of mathematics to multiply two numbers. You have to keep in your mind that, Measurement of the Length is always greater than the measurement Breadth. Now Let's see how we can calculate the Area of Rectangle in java.
Objective:
Write a java program to calculate the area of the Rectangle.
Sample Input
Input Length = 7
Input Breadth = 8
Area of Rectangle: 56
1. Simple Way
Code
public class AreaofRectangle {
public static void main(String args[]){
int Length = 7;
int Breadth = 8;
int c;
c = Length*Breadth;
System.out.println("Area of Rectangle: " + c);
}
}
Output
Area of Rectangle: 56
2. Using Scanner Class
Here we are calculating the area of the rectangle in Java using Scanner Class. Scanner class basically use for taking the input from the users. By using the scanner class we can enter the values according to our choice.
Code
import java.util.Scanner;
public class AreaofRectangle {
public static void main(String args[]){
Scanner sc = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter the Length Measurement");
int Length = sc.nextInt();
System.out.println("Enter the Breadth Measurement");
int Breadth = sc.nextInt();
int c;
c = Length*Breadth;
System.out.println("Product of the Two Numbers is: " + c);
}
}
Output
Enter the Length Measurement
6
Enter the Breadth Measurement
5
Product of the Two Numbers is: 30

0 Comments