Hypotenuse calculation using formulas with power and square root – Computer Sir Ki Class

Code Learning #JAVA#3784

## Hypotenuse calculation using formulas with power and square root

Creating formulas that contain powers and square root.

## Learning Objectives

• Using formulas with powers and roots.
• Learning to build entire formula on right hand side only.

## Source Code

 class PowRoot { public static void main(String[] args) { double b,h,hy ; b=4.2; h=3.3; hy=Math.sqrt(Math.pow(b,2)+Math.pow(h,2)); hy=(int)(hy*100)/100.0; System.out.println("RA triangle given base = "+b+" height = "+h); System.out.println("Computed hypotenuse = "+hy); } } .linedwrap { border: 1px solid #c0c0c0; padding-top: 5px; padding-bottom: 5px; border-radius: 4px; background-color:#F8F8F8; } .linedtextarea { padding: 0px; margin: 0px; background-color:#F8F8F8; } .linedtextarea textarea, .linedwrap .codelines .lineno { font-size: 12pt; font-family: monospace; line-height: normal !important; } .linedtextarea textarea { padding-right:0.3em; padding-top:0.3em; border: 0; } .linedwrap .lines { margin-top: 0px; width: 20px; float: left; overflow: hidden; border-right: 1px solid #c0c0c0; margin-right: 10px; } .linedwrap .codelines { padding-top: 5px; } .linedwrap .codelines .lineno { color:#AAAAAA; padding-right: 2px; padding-top: 0.0em; text-align: right; white-space: nowrap; } .linedwrap .codelines .lineselect { color: red; }

## Run Output

``````RA triangle given base = 4.2 height = 3.3
Computed hypotenuse = 5.34
``````

## Code Understanding

double b,h,hy ; b=4.2; h=3.3;
Double variables declared and b and h are provided initial values. When you learn to collect input you can collect such values from user.

hy=Math.sqrt(Math.pow(b,2)+Math.pow(h,2));
This is java representation of a popular formula c2=a2+b2 . Since left hand side can not contain an expression so we build the entire formula on the right hand side only. For this the entire formula will be square root of a square + b square. For this reason the java math library metods sqrt() and pow() has been used. These methods are a part of java.lang.math package which is included by default in most programming environments as java.lang.* is usually imported by default.

hy=(int)(hy*100)/100.0;
This is done to truncate a high precision double computed value to 2 decimals. We multiply by 100 convert to integer and then divide by double 100.0 so that result is stored in double.

System.out.println(“RA triangle given base = “+b+” height = “+h);
System.out.println(“Computed hypotenuse = “+hy);
Given and computed values are printed.

## Notes

• The hypotenuse calculation will be approximate only due to limitations of precision of such calculations being in double precision. With the use of decimal place truncation or rounding off these values may be further approximated.
• Instead of truncation one can also use rounding off methods in java such by using DecimalFormat pattern based rounding or Math.round() methods. A good link to see such examples is –
https://www.mkyong.com/java/how-to-round-double-float-value-to-2-decimal-points-in-java/

Suggested Filename(s): PowRoot.java

sunmitra| Created: 6-Mar-2018 | Updated: 6-Mar-2018|

×