**Calculate area of Triangle in Java***calculate Area of Triangle*, accept input from User and display output in Console is one of the frequently asked homework question. This is not a tough programming exercise but a good exercise for beginners and anyone who has started working in Java. For calculating area of triangle using formula 1/2(base*height), you need to use arithmetic operator. By doing this kind of exercise you will understand how arithmetic operators works in Java, subtle details which affect calculation like precedence and associativity of operator etc. Fortunately this question is not very popular on Java interview like other exercise we have seen e.g. Java program to print Fibonacci series and Java program to check for palindrome. Nevertheless its a good Java homework.

##
__How to calculate area of Triangle in Java__

In this section we will see complete code example of

*How to calculate area of Triangle in Java*. We are going to use classic formula 1/2(base*height), where base and height will be accepted as user input using java.util.Scanner class. Arithmetic operator used in this exercise is multiplication and division i.e * and /.
/**

*

*

* Formula for calculating area of Triangle is 1/2(base*height)

*

* @author Javin Paul

*/

}

}

}

Please enter base width of Triangle

20

Please enter height of Triangle

30

*

*

**Java program to calculate area of Triangle**.* Formula for calculating area of Triangle is 1/2(base*height)

*

* @author Javin Paul

*/

**public****class**AreaOfTriangle {**public****static****void**main(**String**args[]) {**Scanner**scanner =**new****Scanner**(**System**.in);**System**.out.println("Please enter base width of Triangle ");**float**base = scanner.nextFloat();**System**.out.println("Please enter height of Triangle ");**float**height = scanner.nextFloat();*//calculating area of Triangle in Java***float**area = area(base, height);**System**.out.println("Area of Triangle calculated by Java program is : " + area);}

**public****static****float**area(**float**base,**float**height){**return**(base* height)/2;}

}

**Output**Please enter base width of Triangle

20

Please enter height of Triangle

30

**Area**of Traingle calculated by Java program is : 300.0That's the whole Java program to calculate area of triangle and Now let me introduce with one of the most common mistake Java programmer make while writing this code. If you look closely to area() method you will find that we have wrapped code into small bracket i.e. (base* height)/2 . We have done that to increase precedence, anything which is inside bracket will be executed first. Just change the code, like you write in notebook e.g. 1/2*base*height and suddenly your calculate area for triangle become zero, as shown below :

**public**

**static**

**float**area(

**float**base,

**float**height){

**return**1/2*base*height;

}

Please enter base width of Triangle

10

Please enter height of Triangle

40

**Area**of Traingle calculated by Java program is : 0.0

For a beginner in Java, this is
not an easy bug to find or spot on. In order to find the bug you must know how
Java evaluates an arithmetic expression. Here two arithmetic operator * and
/ are used which are right associative i.e. evaluated from right to left and of
same precedence. first expression which is evaluated by Java is 1/2 which is
division of two integer and result of this would be an integer i.e. zero and
not 0.5, which is making whole area zero. This is happening because of both operands of division operator(/) is
integer, if any of them is float or double
then result would be a floating point number. by combining (base*height) in bracket
we ensure that it executed first and produce a floating point number which can
be divided by two. This kind of simple Java
mistakes can only be avoided by learning and writing such kind of program.
That's why programs like How to calculate square root in Java should be in list
of Java learner.

That's all on How to calculate area of Triangle in Java. We have seen
complete code and also analyzed possible but and How arithmetic expression is
evaluate in Java especially multiplication and division operator.

Other

**Java programming and homework**exercise
## 5 comments :

Awesome code.... good thinking..... Now I realise why area always came 0.0..... but sir,

I then tried (double)(1/2)*base*height; but still it is coming 0.0.... can you tell me why is it coming like that?

how do you calculate area of triangle if only co-ordinates of vertices are given and no base or height is provided?

You can use following formula to calculate area of triangle with co-ordinates of all three vertices given

area = (x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2))/2

import java.util.Scanner;

/**

* Calculates area of triangle in Java given its base and vertical height.

* @author jj

*/

public class AreaOfTriangle1 {

public static void main(String[] args) {

Scanner sn = new Scanner(System.in);

System.out.println("Enter base of triangle:");

double base = sn.nextDouble();

System.out.println("Enter vertical height of triangle:");

double verticalHeight = sn.nextDouble();

AreaOfTriangle1 at = new AreaOfTriangle1();

double area = at.calculateArea(base,verticalHeight);

System.out.println("Area = "+area);

}

/**

* Calculates area of a triangle using base and vertical height

* @param base

* @param verticalHeight

* @return

*/

private double calculateArea(double base, double verticalHeight) {

return base*verticalHeight/2;

}

}

When (1/2) is been operated,it's resulting 0.5 and ....the whole part of it ie 0 is been considered by the compiler ,that's why it's returning 0 and 0.0 is because of 'double'

## Post a Comment