by Dinesh Thakur

Here is the Java Example for Area of Triangle

class AreaofTriangle

{
            public  static  void  main  (String  args [ ] )
            {
                        int b,h;
                        float  a;
                        b =17;
                        h=23;
                        a= (float) 1/2*b*h;
                        System.out.println("Area  of  Triangle  is  "+a);
            }
 
}

Area of Triangle in Java

(float) before 1/2 is used for typecasting. If we divide two integers, the result is also as integer. The benefit of using (float) is that it converts 1/2 into 1.0/2.0 and obviously we get the result as 0.5. If we don't typecast 1/2, then the result of division of two integers is also an integer, hence the result of ½ comes out to be 0, so it is compulsory to typecast an expression which has a / (divide sign).