Methods in Math class are used to perform basic operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
List of some methods in Math class :
NOTE : All the above methods are static .
Here is an example program :
Math class demo import java.lang.Math;
class MathDemo { public static void main(String arg[]) { int num = 9; if (Math.sqrt(num) * Math.sqrt(num) == num) // LINE A System.out.println(num + " is a perfect square"); else System.out.println(num + " is not a perfect square"); num = -2; System.out.println("Value of num : " + Math.abs(num)); // LINE B System.out.println("value when Math.ceil is used : " + Math.ceil(Math.PI)); // LINE C System.out.println("Value when Math.floor is used : " + Math.floor(Math.PI)); // LINE D num = (int) (Math.random() * 10); // LINE E System.out.println("Random number between 0 and 10 : " + num); } } OUTPUT9 is a perfect square Value of num : 2 value when used Math.ceil : 4.0 Value when used Math.floor : 3.0 Random number between 0 and 10 : 5 DESCRIPTIONAt LINE A , using sqrt() method the square root of the number is found and is squared to check whether it is a perfect square or not.
At LINE B , the absolute value of a number is returned by abs() method.
At LINE C , the number is rounded off to ceil.
At LINE D , the number is rounded off to floor.
At LINE E , a random number is returned by random() method. THINGS TO TRY - Find angle between two points using the
Math.cos() method and check whether the angle is acute, right angled or obtuse.
- Find the greater of two numbers, using
Math.max
- Convert an angle in radians to degrees using
Math.toDegree()
Number Method Test2 class NumberMethodTest2 { public static void main(String arg[]) { Float b = 5.6f; System.out.println("Converts to Positive Number : " + Math.abs(-7)); System.out.println("Smallest integer greater than b : " + Math.ceil(b)); System.out.println("Greatest integer less than b : " + Math.floor(b)); System.out.println("Closest integer as double : " + Math.rint(b)); System.out.println("Closest integer : " + Math.round(b)); System.out.println("Minimum of two numbers : " + Math.min(12.3, 9.6)); System.out.println("Maximum of two numbers : " + Math.max(8, 6)); System.out.println("e power 100 : " + Math.exp(100)); System.out.println("Log value : " + Math.log(2.7183)); System.out.println("2 Power 5 : " + Math.pow(2, 5)); System.out.println("Square root : " + Math.sqrt(81)); } } OUTPUTConverts to Positive Number : 7 Smallest integer greater than b : 6.0 Greatest integer less than b : 5.0 Closest integer as double : 6.0 Closest integer : 6 Minimum of two numbers : 9.6 Maximum of two numbers : 8 e power 100 : 2.6881171418161356E43 Log value : 1.0000066849139877 2 Power 5 : 32.0 Square root : 9.0 DESCRIPTIONIn this program, abs, ceil, floor, rint, round, min, max, exp, log, pow and sqrt methods are used.
Number Method Test3 class NumberMethodTest3 { public static void main(String arg[]) { Double degree = 30.0; System.out.println("sin(30) : " + Math.sin(Math.toRadians(degree))); System.out.println("cos(30) : " + Math.cos(Math.toRadians(degree))); System.out.println("tan(30) : " + Math.tan(Math.toRadians(degree))); System.out.println("sec(30) : " + Math.asin(Math.toRadians(degree))); System.out.println("cosec(30) : " + Math.acos(Math.toRadians(degree))); System.out.println("cot(30) : " + Math.atan(Math.toRadians(degree))); System.out.println("Theta of rectangle co-ordinate : " + Math.atan2(45,30)); } } OUTPUTsin(30) : 0.49999999999999994 cos(30) : 0.8660254037844387 tan(30) : 0.5773502691896257 sec(30) : 0.5510695830994463 cosec(30) : 1.0197267436954502 cot(30) : 0.48234790710102493 Theta of rectangle co-ordinate : 0.982793723247329 DESCRIPTIONIn this program, Double value is converted into radians and then sin, cos, tan, asin, acos, atan and atan2 methods are used. THINGS TO TRY - Change the degree value of
30.0 to 45.0, 60.0, 90.0 and see the output difference.
|