To find the perimeter of rectangle we need to have the height and the width. So we create Scanner and we require the user input two numbers. Then we use the formula for the perimeter of rectangle: 2 * (width + height)
At the end we print the result.
import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Write two numbers for two sides of the rectangle:"); Scanner scanner = new Scanner(System.in); int sideWidth = scanner.nextInt(); int sideHeight = scanner.nextInt(); // 2 (w + h) int perimeter = 2 * (sideWidth + sideHeight); System.out.println("Perimeter of rectangle is: " + perimeter); } }
Write two numbers for two sides of the rectangle:
10
20
Perimeter of rectangle is: 60