Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

problem description: write a java program that calculates the area of a…
15,010 Learners found this answer helpful

Question

problem description:
write a java program that calculates the area of a right triangle.
the program should:

  1. ask the user to enter the base and height of the triangle.
  2. compute the area using the formula:
  3. area = \frac{1}{2} \times base \times heightarea = 0.5 \times base \times height

display the calculated area
sample input / output
enter the base of the triangle: 10
enter the height of the triangle: 5
the area of the triangle is: 25.0
step - by - step hints for students

  1. step 1 — read input:
  • use a scanner to read input from the keyboard.
  • you need two variables: base and height (use double type for accuracy).
  1. step 2 — apply the formula:
  • use the formula area = 0.5 base height; to compute the area.
  1. step 3 — display the result:
  • use system.out.println to print the area with a meaningful message.

use the editor to format your answer
last saved 4:06:00 pm

Explanation:

Step1: Import Scanner class

import java.util.Scanner;

Step2: Create Scanner object and declare variables

Scanner scanner = new Scanner(System.in);
double base, height, area;

Step3: Read base and height

System.out.print("Enter the base of the triangle: ");
base = scanner.nextDouble();
System.out.print("Enter the height of the triangle: ");
height = scanner.nextDouble();

Step4: Calculate area

area = 0.5 * base * height;

Step5: Display the result

System.out.println("The area of the triangle is: " + area);

Answer:

import java.util.Scanner;

public class TriangleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double base, height, area;

        System.out.print("Enter the base of the triangle: ");
        base = scanner.nextDouble();
        System.out.print("Enter the height of the triangle: ");
        height = scanner.nextDouble();

        area = 0.5 * base * height;

        System.out.println("The area of the triangle is: " + area);

        scanner.close();
    }
}