Write a Java program to find area of rectangle using variable length arguments
Answers
Area of rectangle
Output:
enter Length: 3
enter Width: 5
Area of Rectangle :15.0
Explanation:
The program to calculate area of rectangle using java language can be given as follows:
Program:
//defining package
import java.util.*; //import package for user input
class Main //defining class Main
{
public static void area(double length,double width) //defining method area
{
double area; //defining variable area
area= length*width; //calculate area of Rectangle using formula
System.out.println("Area of Rectangle :"+area); //print area
}
public static void main (String[] args) //defining main method
{
double length,width; //defining variable
Scanner ob1 = new Scanner(System.in); //creating Scanner class object
System.out.print("enter Length: "); //message
length = ob1.nextDouble(); //taking input from user
System.out.print("enter Width: "); //message
width = ob1.nextDouble(); //taking input from user
area(length,width); //calling function
}
}
The description of the above program as follows:
- In above java program, a package is used for taking input from user. Then a class is defined that is "Main", inside a class two method is declare that is "area method and main method".
- The area method takes two variables as a function parameter and inside a function, a double variable area is declared, that is used for calculating the area of a rectangle and print its value.
- In the main method, two double variable is declared that is "length and width". then a scanner class object "ob1" is created, that is used for taking variable value from the user side and in the main method, the area method is a call that takes these variables as method parameters.
Learn More:
- Formula of area of rectangle: https://brainly.in/question/1667375
- What is the area of rectangle: https://brainly.in/question/6157513