How To Find Minimum Height of the Triangle with Given Base and Area in Java?


We have the area of triangle ‘a’ and base ‘b’. As per problem statement we have to find the minimum height ‘h’ by using Java programming language.

As we know area of triangle, when base and height are given −

$$\mathrm{area \:=\: \frac{1}{2}\: * \:base\: *\: height}$$

By using above formula, we can get height from it −

height = (2 * area) / base

Then by using the inbuilt ceil() method we can get the minimum height.

To show you some instances

Instance-1

Suppose, given area = 12 and base = 6

Then by using the formula to get height,

Minimum height = 4.0

Instance-2

Suppose, given area = 8 and base = 4

Then by using the formula to get height,

Minimum height = 4.0

Instance-3

Suppose, given area = 12 and base = 5

Then by using the formula to get height,

Minimum height = 5.0

Syntax

In java, we have Math.ceil() method which is used to get the rounded value to the nearest mathematical integer(means smallest integer) which is greater than or equal to the given floating point number.

Below is the syntax for that.

Math.ceil(double value);

Algorithm

  • Step 1 − Get the area and base value of the triangle either by initialization or by user input.

  • Step 2 − Calculate the height by using the formula.

  • Step 3 − Then find the minimum height by using Math.ceil() method.

  • Step 4 − Print the result.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using User Static Value

In this approach the base and area value of the triangle will be declared in the program then by using the algorithm find the minimum height of the triangle.

Example

import java.util.*; import java.io.*; public class Main { //main method public static void main(String args[]){ //Declared the area of triangle double area = 6; System.out.println("Area of triangle: "+area); //Declared the base of triangle double base = 14; System.out.println("Base of triangle: "+base); //Find height of triangle double height = (2 * area) / base; System.out.println("Height: " + height); //Find minimum height of triangle by using ceil() method double minHeight = Math.ceil(height); System.out.println("Minimum height: " + minHeight); } }

Output

Area of triangle: 6.0
Base of triangle: 14.0
Height: 0.8571428571428571
Minimum height: 1.0

Approach-2: By Using User Defined Method

In this approach the base and area value of the triangle will be declared in the program. Then call a user defined method by passing this base and area as parameters.

Inside the method, find the minimum height of the triangle by using formula.

Example

import java.util.*; import java.io.*; public class Main{ //main method public static void main(String args[]){ //Declared the area of triangle double area = 12; System.out.println("Area of triangle: "+area); //Declared the base of triangle double base = 6; System.out.println("Base of triangle: "+base); //calling a user defined method findHeight(area,base); } //user defined method public static void findHeight(double area, double base){ //Find height of triangle double height = (2 * area) / base; System.out.println("Height: " + height); //Find minimum height of triangle by using ceil() method double minHeight = Math.ceil(height); System.out.println("Minimum height: " + minHeight); } }

Output

Area of triangle: 12.0
Base of triangle: 6.0
Height: 4.0
Minimum height: 4.0

In this article, we explored how to calculate the minimum height of the triangle when base and area are given in Java by using different approaches.

Updated on: 28-Oct-2022

462 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements