by Dinesh Thakur Category: Operators and Expressions

Here is the Java Example for Area of Triangle

class AreaofTriangle

            public  static  void  main  (String  args [ ] )
                        int b,h;
                        float  a;
                        b =17;
                        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).

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.