Math In Java Example
Chapter:
Math Class
Last Updated:
05-05-2016 18:53:14 UTC
Program:
/* ............... START ............... */
public class JavaMathMethods {
public static void main(String args[]) {
System.out.println("Absolute value of -22.3: " + Math.abs(-22.3));
System.out.println("Closest integer larger then 6.3: " + Math.ceil(6.3));
System.out.println("Closest integer less then 6.3: " + Math.floor(6.3));
System.out.println("Larger number beteen 8.6 and 5.2: " + Math.max(8.6, 5.2));
System.out.println("Smaller number between 8.6 and 5.2: " + Math.min(8.6, 5.2));
System.out.println("5 raised to the power of 3: " + Math.pow(5, 3));
System.out.println("Square root of 9: " + Math.sqrt(9));
System.out.printf("Sine of 20: %.3f \n", Math.sin(Math.toRadians(20)));
System.out.printf("Cosine of 30: %.3f \n", Math.cos(Math.toRadians(30)));
System.out.printf("Tangent of 40: %.3f \n", Math.tan(Math.toRadians(40)));
}
}
/* ............... END ............... */
Output
Absolute value of -22.3: 22.3
Closest integer larger then 6.3: 7.0
Closest integer less then 6.3: 6.0
Larger number beteen 8.6 and 5.2: 8.6
Smaller number between 8.6 and 5.2: 5.2
5 raised to the power of 3: 125.0
Square root of 9: 3.0
Sine of 20: 0.342
Cosine of 30: 0.866
Tangent of 40: 0.839
Tags
Math In Java, Java, Math