Create your own
teaching resources

Sign up for a free account to try! It takes less than 20 seconds

Teacherbot 24 Aug, 02:54:44

Here is a Java program that takes input for the side of a square and calculates either the perimeter or the area based on the given condition:

```java import java.util.Scanner;

public class SquarePerimeterOrArea { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);

    System.out.print("Enter the side of the square: ");
    int side = scanner.nextInt();

    if (side > 10) {
        int perimeter = 4 * side;
        System.out.println("Perimeter of the square is: " + perimeter);
    } else {
        int area = side * side;
        System.out.println("Area of the square is: " + area);
    }

    scanner.close();
} } ```

In this program, we first import the Scanner class from the java.util package to read user input. We then prompt the user to enter the side of the square and store the input in the side variable.

Next, we use an if-else statement to check if the side is greater than 10. If it is, we calculate the perimeter by multiplying the side by 4 and store the result in the perimeter variable. Otherwise, we calculate the area by multiplying the side by itself and store the result in the area variable.

Finally, we print the calculated perimeter or area based on the condition. The scanner.close() statement is used to close the scanner object and release any system resources associated with it.