Covert the following Java program to a Python program:
import java.util.Scanner;
/* Calculates and displays the area of a rectangle
* based on the width and length entered by the user.
*/
public class RectangleArea2 {
public static void main(String[] args) {
int length; //longer side of rectangle
int width; //shorter side of rectangle
int area; //calculated area of rectangle
Scanner input = new Scanner(System.in);
System.out.print("Enter the length: ");
length = input.nextInt();
System.out.print("Enter the width: ");
width = input.nextInt(); input.close();
area = length * width;
System.out.println("Area of rectangle: " + area);
}
}
The given java program is to find the area of a rectangle. The value for length and width is accepted from user and is used to calculate the area. Finally the area value is displayed. The required python code and corresponding output are as follows:
# Program to find the Area of a Rectangle
length = int(input('Enter the Length: '))
width = int(input('Enter the Width: '))
# calculate the area
area = length*width
print("\n Area of Rectangle: %d" %area)
Output:
Get Answers For Free
Most questions answered within 1 hours.